jpayne@69: """ jpayne@69: OptionMenu widget modified to allow dynamic menu reconfiguration jpayne@69: and setting of highlightthickness jpayne@69: """ jpayne@69: import copy jpayne@69: jpayne@69: from tkinter import OptionMenu, _setit, StringVar, Button jpayne@69: jpayne@69: class DynOptionMenu(OptionMenu): jpayne@69: """ jpayne@69: unlike OptionMenu, our kwargs can include highlightthickness jpayne@69: """ jpayne@69: def __init__(self, master, variable, value, *values, **kwargs): jpayne@69: # TODO copy value instead of whole dict jpayne@69: kwargsCopy=copy.copy(kwargs) jpayne@69: if 'highlightthickness' in list(kwargs.keys()): jpayne@69: del(kwargs['highlightthickness']) jpayne@69: OptionMenu.__init__(self, master, variable, value, *values, **kwargs) jpayne@69: self.config(highlightthickness=kwargsCopy.get('highlightthickness')) jpayne@69: #self.menu=self['menu'] jpayne@69: self.variable=variable jpayne@69: self.command=kwargs.get('command') jpayne@69: jpayne@69: def SetMenu(self,valueList,value=None): jpayne@69: """ jpayne@69: clear and reload the menu with a new set of options. jpayne@69: valueList - list of new options jpayne@69: value - initial value to set the optionmenu's menubutton to jpayne@69: """ jpayne@69: self['menu'].delete(0,'end') jpayne@69: for item in valueList: jpayne@69: self['menu'].add_command(label=item, jpayne@69: command=_setit(self.variable,item,self.command)) jpayne@69: if value: jpayne@69: self.variable.set(value) jpayne@69: jpayne@69: def _dyn_option_menu(parent): # htest # jpayne@69: from tkinter import Toplevel # + StringVar, Button jpayne@69: jpayne@69: top = Toplevel(parent) jpayne@69: top.title("Tets dynamic option menu") jpayne@69: x, y = map(int, parent.geometry().split('+')[1:]) jpayne@69: top.geometry("200x100+%d+%d" % (x + 250, y + 175)) jpayne@69: top.focus_set() jpayne@69: jpayne@69: var = StringVar(top) jpayne@69: var.set("Old option set") #Set the default value jpayne@69: dyn = DynOptionMenu(top,var, "old1","old2","old3","old4") jpayne@69: dyn.pack() jpayne@69: jpayne@69: def update(): jpayne@69: dyn.SetMenu(["new1","new2","new3","new4"], value="new option set") jpayne@69: button = Button(top, text="Change option set", command=update) jpayne@69: button.pack() jpayne@69: jpayne@69: if __name__ == '__main__': jpayne@69: from idlelib.idle_test.htest import run jpayne@69: run(_dyn_option_menu)