IDLE: Simplify DynOptionsMenu __init__code (GH-101371)

Refactor DynOptionMenu's initializer to not copy  kwargs dict and use subscripting;
improve its htest.

(cherry picked from commit c41af812c9)

Co-authored-by: JosephSBoyle <48555120+JosephSBoyle@users.noreply.github.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
Miss Islington (bot) 2023-02-27 22:36:34 -08:00 committed by GitHub
parent ca1cc1484f
commit 2701a49df2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 14 deletions

View File

@ -2,22 +2,17 @@
OptionMenu widget modified to allow dynamic menu reconfiguration OptionMenu widget modified to allow dynamic menu reconfiguration
and setting of highlightthickness and setting of highlightthickness
""" """
import copy
from tkinter import OptionMenu, _setit, StringVar, Button from tkinter import OptionMenu, _setit, StringVar, Button
class DynOptionMenu(OptionMenu): class DynOptionMenu(OptionMenu):
""" """Add SetMenu and highlightthickness to OptionMenu.
unlike OptionMenu, our kwargs can include highlightthickness
Highlightthickness adds space around menu button.
""" """
def __init__(self, master, variable, value, *values, **kwargs): def __init__(self, master, variable, value, *values, **kwargs):
# TODO copy value instead of whole dict highlightthickness = kwargs.pop('highlightthickness', None)
kwargsCopy=copy.copy(kwargs)
if 'highlightthickness' in list(kwargs.keys()):
del(kwargs['highlightthickness'])
OptionMenu.__init__(self, master, variable, value, *values, **kwargs) OptionMenu.__init__(self, master, variable, value, *values, **kwargs)
self.config(highlightthickness=kwargsCopy.get('highlightthickness')) self['highlightthickness'] = highlightthickness
#self.menu=self['menu']
self.variable = variable self.variable = variable
self.command = kwargs.get('command') self.command = kwargs.get('command')
@ -38,14 +33,15 @@ def _dyn_option_menu(parent): # htest #
from tkinter import Toplevel # + StringVar, Button from tkinter import Toplevel # + StringVar, Button
top = Toplevel(parent) top = Toplevel(parent)
top.title("Tets dynamic option menu") top.title("Test dynamic option menu")
x, y = map(int, parent.geometry().split('+')[1:]) x, y = map(int, parent.geometry().split('+')[1:])
top.geometry("200x100+%d+%d" % (x + 250, y + 175)) top.geometry("200x100+%d+%d" % (x + 250, y + 175))
top.focus_set() top.focus_set()
var = StringVar(top) var = StringVar(top)
var.set("Old option set") #Set the default value var.set("Old option set") #Set the default value
dyn = DynOptionMenu(top,var, "old1","old2","old3","old4") dyn = DynOptionMenu(top, var, "old1","old2","old3","old4",
highlightthickness=5)
dyn.pack() dyn.pack()
def update(): def update():
@ -54,5 +50,6 @@ def update():
button.pack() button.pack()
if __name__ == '__main__': if __name__ == '__main__':
# Only module without unittests because of intention to replace.
from idlelib.idle_test.htest import run from idlelib.idle_test.htest import run
run(_dyn_option_menu) run(_dyn_option_menu)