Python / Tkinter Is there a way to distinguish a programmatic change to a combo box from a user initiated change? -
i'm building gui front-end network appliance many of parameters (parameter1 , parameter2 example) best displayed combo box. there implicit hierarchy of parameters parameter1 change value of parameter2, parameter2 not change parameter1 , parameter2 may changed independently.
i can trap user initiated change parameter1 , parameter2, when user initiated change parameter1 causes programmatic change parameter2 trap on parameter2 invoked.
is there clever way work around this?
sample code:
import tkinter tk import ttk global parameter1 global parameter2 global list1 global list2 list1 = ["apple","bananna","orange"] list2 = ["moe","larry","curly"] def parameter1_changed(arg1,arg2,arg3): print "selection list 1 changed" print "arg1=%s arg2=%s arg3=%s"%(arg1,arg2,arg3) parameter2.set(list2[1]) def parameter2_changed(arg1,arg2,arg3): print "selection list 2 changed" print "arg1=%s arg2=%s arg3=%s"%(arg1,arg2,arg3) root = tk.tk() parameter1 = tk.stringvar() parameter1.set(list1[0]) parameter2=tk.stringvar() parameter2.set(list2[0]) tk.label(root,text="list #1").grid(row=0,column=0,sticky=tk.e) ttk.combobox(root,values=list1,textvariable=parameter1,state='readonly').grid(row=0,column=1,sticky=tk.w) tk.label(root,text="list #2").grid(row=1,column=0,sticky=tk.e) ttk.combobox(root,values=list2,textvariable=parameter2,state='readonly').grid(row=1,column=1,sticky=tk.w) parameter1.trace("w",parameter1_changed) parameter2.trace("w",parameter2_changed) root.mainloop()
you create subclass of stringvar
allows disable callback triggers:
import tkinter tk import ttk global parameter1 global parameter2 global list1 global list2 class suppressablestingvar(tk.stringvar): def __init__(self, *args, **kargs): tk.stringvar.__init__(self, *args, **kargs) self.silenced = false def trace(self, mode, callback): conditional_callback = lambda *args, **kargs: callback(*args, **kargs) if not self.silenced else none tk.stringvar.trace(self, mode, conditional_callback) def set(self, value, suppress_callback = false): if suppress_callback: self.silenced = true tk.stringvar.set(self, value) self.silenced = false list1 = ["apple","bananna","orange"] list2 = ["moe","larry","curly"] def parameter1_changed(arg1,arg2,arg3): print "selection list 1 changed" print "arg1=%s arg2=%s arg3=%s"%(arg1,arg2,arg3) parameter2.set(list2[1], true) def parameter2_changed(arg1,arg2,arg3): print "selection list 2 changed" print "arg1=%s arg2=%s arg3=%s"%(arg1,arg2,arg3) root = tk.tk() parameter1 = tk.stringvar() parameter1.set(list1[0]) parameter2=suppressablestingvar() parameter2.set(list2[0]) tk.label(root,text="list #1").grid(row=0,column=0,sticky=tk.e) ttk.combobox(root,values=list1,textvariable=parameter1,state='readonly').grid(row=0,column=1,sticky=tk.w) tk.label(root,text="list #2").grid(row=1,column=0,sticky=tk.e) ttk.combobox(root,values=list2,textvariable=parameter2,state='readonly').grid(row=1,column=1,sticky=tk.w) parameter1.trace("w",parameter1_changed) parameter2.trace("w",parameter2_changed) root.mainloop()
manipulating first dropdown produces output:
selection list 1 changed arg1=py_var0 arg2= arg3=w
Comments
Post a Comment