Steven M. Gava | f126bcb | 2001-10-26 06:49:14 +0000 | [diff] [blame] | 1 | """ |
| 2 | OptionMenu widget modified to allow dynamic menu reconfiguration |
Steven M. Gava | c034b47 | 2001-11-03 14:55:47 +0000 | [diff] [blame] | 3 | and setting of highlightthickness |
Steven M. Gava | f126bcb | 2001-10-26 06:49:14 +0000 | [diff] [blame] | 4 | """ |
Terry Jan Reedy | 62012fc | 2014-05-24 18:48:03 -0400 | [diff] [blame^] | 5 | from Tkinter import OptionMenu, _setit, Tk, StringVar, Button |
| 6 | |
Steven M. Gava | c034b47 | 2001-11-03 14:55:47 +0000 | [diff] [blame] | 7 | import copy |
Terry Jan Reedy | 62012fc | 2014-05-24 18:48:03 -0400 | [diff] [blame^] | 8 | import re |
Steven M. Gava | f126bcb | 2001-10-26 06:49:14 +0000 | [diff] [blame] | 9 | |
| 10 | class DynOptionMenu(OptionMenu): |
| 11 | """ |
Steven M. Gava | c034b47 | 2001-11-03 14:55:47 +0000 | [diff] [blame] | 12 | unlike OptionMenu, our kwargs can include highlightthickness |
Steven M. Gava | f126bcb | 2001-10-26 06:49:14 +0000 | [diff] [blame] | 13 | """ |
| 14 | def __init__(self, master, variable, value, *values, **kwargs): |
Steven M. Gava | c034b47 | 2001-11-03 14:55:47 +0000 | [diff] [blame] | 15 | #get a copy of kwargs before OptionMenu.__init__ munges them |
| 16 | kwargsCopy=copy.copy(kwargs) |
| 17 | if 'highlightthickness' in kwargs.keys(): |
| 18 | del(kwargs['highlightthickness']) |
Steven M. Gava | f126bcb | 2001-10-26 06:49:14 +0000 | [diff] [blame] | 19 | OptionMenu.__init__(self, master, variable, value, *values, **kwargs) |
Steven M. Gava | c034b47 | 2001-11-03 14:55:47 +0000 | [diff] [blame] | 20 | self.config(highlightthickness=kwargsCopy.get('highlightthickness')) |
Steven M. Gava | f126bcb | 2001-10-26 06:49:14 +0000 | [diff] [blame] | 21 | #self.menu=self['menu'] |
| 22 | self.variable=variable |
| 23 | self.command=kwargs.get('command') |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 24 | |
Steven M. Gava | 41a8532 | 2001-10-29 08:05:34 +0000 | [diff] [blame] | 25 | def SetMenu(self,valueList,value=None): |
Steven M. Gava | f126bcb | 2001-10-26 06:49:14 +0000 | [diff] [blame] | 26 | """ |
| 27 | clear and reload the menu with a new set of options. |
| 28 | valueList - list of new options |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 29 | value - initial value to set the optionmenu's menubutton to |
Steven M. Gava | f126bcb | 2001-10-26 06:49:14 +0000 | [diff] [blame] | 30 | """ |
| 31 | self['menu'].delete(0,'end') |
| 32 | for item in valueList: |
| 33 | self['menu'].add_command(label=item, |
| 34 | command=_setit(self.variable,item,self.command)) |
Steven M. Gava | 41a8532 | 2001-10-29 08:05:34 +0000 | [diff] [blame] | 35 | if value: |
| 36 | self.variable.set(value) |
Terry Jan Reedy | 62012fc | 2014-05-24 18:48:03 -0400 | [diff] [blame^] | 37 | |
| 38 | def _dyn_option_menu(parent): |
| 39 | root = Tk() |
| 40 | root.title("Tets dynamic option menu") |
| 41 | var = StringVar(root) |
| 42 | width, height, x, y = list(map(int, re.split('[x+]', parent.geometry()))) |
| 43 | root.geometry("+%d+%d"%(x, y + 150)) |
| 44 | var.set("Old option set") #Set the default value |
| 45 | dyn = DynOptionMenu(root,var, "old1","old2","old3","old4") |
| 46 | dyn.pack() |
| 47 | |
| 48 | def update(): |
| 49 | dyn.SetMenu(["new1","new2","new3","new4"],value="new option set") |
| 50 | |
| 51 | button = Button(root, text="Change option set", command=update) |
| 52 | button.pack() |
| 53 | root.mainloop() |
| 54 | |
| 55 | if __name__ == '__main__': |
| 56 | from idlelib.idle_test.htest import run |
| 57 | run(_dyn_option_menu) |