blob: 922de96ceac6cdc9f1d73790a18a824a17dea521 [file] [log] [blame]
Steven M. Gavaf126bcb2001-10-26 06:49:14 +00001"""
2OptionMenu widget modified to allow dynamic menu reconfiguration
Steven M. Gavac034b472001-11-03 14:55:47 +00003and setting of highlightthickness
Steven M. Gavaf126bcb2001-10-26 06:49:14 +00004"""
Georg Brandl14fc4272008-05-17 18:39:55 +00005from tkinter import OptionMenu
6from tkinter import _setit
Steven M. Gavac034b472001-11-03 14:55:47 +00007import copy
Steven M. Gavaf126bcb2001-10-26 06:49:14 +00008
9class DynOptionMenu(OptionMenu):
10 """
Steven M. Gavac034b472001-11-03 14:55:47 +000011 unlike OptionMenu, our kwargs can include highlightthickness
Steven M. Gavaf126bcb2001-10-26 06:49:14 +000012 """
13 def __init__(self, master, variable, value, *values, **kwargs):
Steven M. Gavac034b472001-11-03 14:55:47 +000014 #get a copy of kwargs before OptionMenu.__init__ munges them
15 kwargsCopy=copy.copy(kwargs)
Kurt B. Kaisere0712772007-08-23 05:25:55 +000016 if 'highlightthickness' in list(kwargs.keys()):
Steven M. Gavac034b472001-11-03 14:55:47 +000017 del(kwargs['highlightthickness'])
Steven M. Gavaf126bcb2001-10-26 06:49:14 +000018 OptionMenu.__init__(self, master, variable, value, *values, **kwargs)
Steven M. Gavac034b472001-11-03 14:55:47 +000019 self.config(highlightthickness=kwargsCopy.get('highlightthickness'))
Steven M. Gavaf126bcb2001-10-26 06:49:14 +000020 #self.menu=self['menu']
21 self.variable=variable
22 self.command=kwargs.get('command')
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000023
Steven M. Gava41a85322001-10-29 08:05:34 +000024 def SetMenu(self,valueList,value=None):
Steven M. Gavaf126bcb2001-10-26 06:49:14 +000025 """
26 clear and reload the menu with a new set of options.
27 valueList - list of new options
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000028 value - initial value to set the optionmenu's menubutton to
Steven M. Gavaf126bcb2001-10-26 06:49:14 +000029 """
30 self['menu'].delete(0,'end')
31 for item in valueList:
32 self['menu'].add_command(label=item,
33 command=_setit(self.variable,item,self.command))
Steven M. Gava41a85322001-10-29 08:05:34 +000034 if value:
35 self.variable.set(value)