ยปCore Development>Code coverage>Lib/idlelib/dynoption.py

Python code coverage for Lib/idlelib/dynoption.py

#countcontent
1n/a"""
2n/aOptionMenu widget modified to allow dynamic menu reconfiguration
3n/aand setting of highlightthickness
4n/a"""
5n/aimport copy
6n/a
7n/afrom tkinter import OptionMenu, _setit, StringVar, Button
8n/a
9n/aclass DynOptionMenu(OptionMenu):
10n/a """
11n/a unlike OptionMenu, our kwargs can include highlightthickness
12n/a """
13n/a def __init__(self, master, variable, value, *values, **kwargs):
14n/a # TODO copy value instead of whole dict
15n/a kwargsCopy=copy.copy(kwargs)
16n/a if 'highlightthickness' in list(kwargs.keys()):
17n/a del(kwargs['highlightthickness'])
18n/a OptionMenu.__init__(self, master, variable, value, *values, **kwargs)
19n/a self.config(highlightthickness=kwargsCopy.get('highlightthickness'))
20n/a #self.menu=self['menu']
21n/a self.variable=variable
22n/a self.command=kwargs.get('command')
23n/a
24n/a def SetMenu(self,valueList,value=None):
25n/a """
26n/a clear and reload the menu with a new set of options.
27n/a valueList - list of new options
28n/a value - initial value to set the optionmenu's menubutton to
29n/a """
30n/a self['menu'].delete(0,'end')
31n/a for item in valueList:
32n/a self['menu'].add_command(label=item,
33n/a command=_setit(self.variable,item,self.command))
34n/a if value:
35n/a self.variable.set(value)
36n/a
37n/adef _dyn_option_menu(parent): # htest #
38n/a from tkinter import Toplevel # + StringVar, Button
39n/a
40n/a top = Toplevel(parent)
41n/a top.title("Tets dynamic option menu")
42n/a x, y = map(int, parent.geometry().split('+')[1:])
43n/a top.geometry("200x100+%d+%d" % (x + 250, y + 175))
44n/a top.focus_set()
45n/a
46n/a var = StringVar(top)
47n/a var.set("Old option set") #Set the default value
48n/a dyn = DynOptionMenu(top,var, "old1","old2","old3","old4")
49n/a dyn.pack()
50n/a
51n/a def update():
52n/a dyn.SetMenu(["new1","new2","new3","new4"], value="new option set")
53n/a button = Button(top, text="Change option set", command=update)
54n/a button.pack()
55n/a
56n/aif __name__ == '__main__':
57n/a from idlelib.idle_test.htest import run
58n/a run(_dyn_option_menu)