annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/dynoption.py @ 68:5028fdace37b

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