Coverage for dynoption.py: 18%

32 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-11 13:22 -0700

1""" 

2OptionMenu widget modified to allow dynamic menu reconfiguration 

3and setting of highlightthickness 

4""" 

5from tkinter import OptionMenu, _setit, StringVar, Button 

6 

7class DynOptionMenu(OptionMenu): 

8 """Add SetMenu and highlightthickness to OptionMenu. 

9 

10 Highlightthickness adds space around menu button. 

11 """ 

12 def __init__(self, master, variable, value, *values, **kwargs): 

13 highlightthickness = kwargs.pop('highlightthickness', None) 

14 OptionMenu.__init__(self, master, variable, value, *values, **kwargs) 

15 self['highlightthickness'] = highlightthickness 

16 self.variable = variable 

17 self.command = kwargs.get('command') 

18 

19 def SetMenu(self,valueList,value=None): 

20 """ 

21 clear and reload the menu with a new set of options. 

22 valueList - list of new options 

23 value - initial value to set the optionmenu's menubutton to 

24 """ 

25 self['menu'].delete(0,'end') 

26 for item in valueList: 

27 self['menu'].add_command(label=item, 

28 command=_setit(self.variable,item,self.command)) 

29 if value: 

30 self.variable.set(value) 

31 

32def _dyn_option_menu(parent): # htest # 

33 from tkinter import Toplevel # + StringVar, Button 

34 

35 top = Toplevel(parent) 

36 top.title("Test dynamic option menu") 

37 x, y = map(int, parent.geometry().split('+')[1:]) 

38 top.geometry("200x100+%d+%d" % (x + 250, y + 175)) 

39 top.focus_set() 

40 

41 var = StringVar(top) 

42 var.set("Old option set") #Set the default value 

43 dyn = DynOptionMenu(top, var, "old1","old2","old3","old4", 

44 highlightthickness=5) 

45 dyn.pack() 

46 

47 def update(): 

48 dyn.SetMenu(["new1","new2","new3","new4"], value="new option set") 

49 button = Button(top, text="Change option set", command=update) 

50 button.pack() 

51 

52if __name__ == '__main__': 52 ↛ 54line 52 didn't jump to line 54, because the condition on line 52 was never true

53 # Only module without unittests because of intention to replace. 

54 from idlelib.idle_test.htest import run 

55 run(_dyn_option_menu)