annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/dynoption.py @ 69:33d812a61356

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