22OptionMenu widget modified to allow dynamic menu reconfiguration
33and setting of highlightthickness
44"""
5- import copy
6-
75from tkinter import OptionMenu , _setit , StringVar , Button
86
97class DynOptionMenu (OptionMenu ):
10- """
11- unlike OptionMenu, our kwargs can include highlightthickness
8+ """Add SetMenu and highlightthickness to OptionMenu.
9+
10+ Highlightthickness adds space around menu button.
1211 """
1312 def __init__ (self , master , variable , value , * values , ** kwargs ):
14- # TODO copy value instead of whole dict
15- kwargsCopy = copy .copy (kwargs )
16- if 'highlightthickness' in list (kwargs .keys ()):
17- del (kwargs ['highlightthickness' ])
13+ highlightthickness = kwargs .pop ('highlightthickness' , None )
1814 OptionMenu .__init__ (self , master , variable , value , * values , ** kwargs )
19- self .config (highlightthickness = kwargsCopy .get ('highlightthickness' ))
20- #self.menu=self['menu']
21- self .variable = variable
22- self .command = kwargs .get ('command' )
15+ self ['highlightthickness' ] = highlightthickness
16+ self .variable = variable
17+ self .command = kwargs .get ('command' )
2318
2419 def SetMenu (self ,valueList ,value = None ):
2520 """
@@ -38,14 +33,15 @@ def _dyn_option_menu(parent): # htest #
3833 from tkinter import Toplevel # + StringVar, Button
3934
4035 top = Toplevel (parent )
41- top .title ("Tets dynamic option menu" )
36+ top .title ("Test dynamic option menu" )
4237 x , y = map (int , parent .geometry ().split ('+' )[1 :])
4338 top .geometry ("200x100+%d+%d" % (x + 250 , y + 175 ))
4439 top .focus_set ()
4540
4641 var = StringVar (top )
4742 var .set ("Old option set" ) #Set the default value
48- dyn = DynOptionMenu (top ,var , "old1" ,"old2" ,"old3" ,"old4" )
43+ dyn = DynOptionMenu (top , var , "old1" ,"old2" ,"old3" ,"old4" ,
44+ highlightthickness = 5 )
4945 dyn .pack ()
5046
5147 def update ():
@@ -54,5 +50,6 @@ def update():
5450 button .pack ()
5551
5652if __name__ == '__main__' :
53+ # Only module without unittests because of intention to replace.
5754 from idlelib .idle_test .htest import run
5855 run (_dyn_option_menu )
0 commit comments