How to run a code whenever a Tkinter widget value changes? How to run a code whenever a Tkinter widget value changes? tkinter tkinter

How to run a code whenever a Tkinter widget value changes?


I think the correct method is to use trace on a tkinter variable that has been assigned to a widget.

For example...

import tkinterroot = tkinter.Tk()myvar = tkinter.StringVar()myvar.set('')mywidget = tkinter.Entry(root,textvariable=myvar,width=10)mywidget.pack()def oddblue(a,b,c):    if len(myvar.get())%2 == 0:        mywidget.config(bg='red')    else:        mywidget.config(bg='blue')    mywidget.update_idletasks()myvar.trace('w',oddblue)root.mainloop()

The w in trace tells tkinter whenever somebody writes (updates) the variable, which would happen every time someone wrote something in the Entry widget, do oddblue. The trace always passes three values to whatever function you've listed, so you'll need to expect them in your function, hence a,b,c. I usually do nothing with them as everything I need is defined locally anyway. From what I can tell a is the variable object, b is blank (not sure why), and c is the trace mode (i.e.w).

For more info on tkinter variables check this out.


How I would solve this in Tcl would be to make sure that the checkbutton, spinbox and radiobutton widgets are all associated with an array variable. I would then put a trace on the array which would cause a function to be called each time that variable is written. Tcl makes this trivial.

Unfortunately Tkinter doesn't support working with Tcl arrays. Fortunately, it's fairly easy to hack in. If you're adventurous, try the following code.

From the full disclosure department: I threw this together this morning in about half an hour. I haven't actually used this technique in any real code. I couldn't resist the challenge, though, to figure out how to use arrays with Tkinter.

import Tkinter as tkclass MyApp(tk.Tk):    '''Example app that uses Tcl arrays'''    def __init__(self):        tk.Tk.__init__(self)        self.arrayvar = ArrayVar()        self.labelvar = tk.StringVar()        rb1 = tk.Radiobutton(text="one", variable=self.arrayvar("radiobutton"), value=1)        rb2 = tk.Radiobutton(text="two", variable=self.arrayvar("radiobutton"), value=2)        cb = tk.Checkbutton(text="checked?", variable=self.arrayvar("checkbutton"),                              onvalue="on", offvalue="off")        entry = tk.Entry(textvariable=self.arrayvar("entry"))        label = tk.Label(textvariable=self.labelvar)        spinbox = tk.Spinbox(from_=1, to=11, textvariable=self.arrayvar("spinbox"))        button = tk.Button(text="click to print contents of array", command=self.OnDump)        for widget in (cb, rb1, rb2, spinbox, entry, button, label):            widget.pack(anchor="w", padx=10)        self.labelvar.set("Click on a widget to see this message change")        self.arrayvar["entry"] = "something witty"        self.arrayvar["radiobutton"] = 2        self.arrayvar["checkbutton"] = "on"        self.arrayvar["spinbox"] = 11        self.arrayvar.trace(mode="w", callback=self.OnTrace)    def OnDump(self):        '''Print the contents of the array'''        print self.arrayvar.get()    def OnTrace(self, varname, elementname, mode):        '''Show the new value in a label'''        self.labelvar.set("%s changed; new value='%s'" % (elementname, self.arrayvar[elementname]))class ArrayVar(tk.Variable):    '''A variable that works as a Tcl array variable'''    _default = {}    _elementvars = {}    def __del__(self):        self._tk.globalunsetvar(self._name)        for elementvar in self._elementvars:            del elementvar    def __setitem__(self, elementname, value):        if elementname not in self._elementvars:            v = ArrayElementVar(varname=self._name, elementname=elementname, master=self._master)            self._elementvars[elementname] = v        self._elementvars[elementname].set(value)    def __getitem__(self, name):        if name in self._elementvars:            return self._elementvars[name].get()        return None    def __call__(self, elementname):        '''Create a new StringVar as an element in the array'''        if elementname not in self._elementvars:            v = ArrayElementVar(varname=self._name, elementname=elementname, master=self._master)            self._elementvars[elementname] = v        return self._elementvars[elementname]    def set(self, dictvalue):        # this establishes the variable as an array         # as far as the Tcl interpreter is concerned        self._master.eval("array set {%s} {}" % self._name)         for (k, v) in dictvalue.iteritems():            self._tk.call("array","set",self._name, k, v)    def get(self):        '''Return a dictionary that represents the Tcl array'''        value = {}        for (elementname, elementvar) in self._elementvars.iteritems():            value[elementname] = elementvar.get()        return valueclass ArrayElementVar(tk.StringVar):    '''A StringVar that represents an element of an array'''    _default = ""    def __init__(self, varname, elementname, master):        self._master = master        self._tk = master.tk        self._name = "%s(%s)" % (varname, elementname)        self.set(self._default)    def __del__(self):        """Unset the variable in Tcl."""        self._tk.globalunsetvar(self._name)if __name__ == "__main__":    app=MyApp()    app.wm_geometry("400x200")    app.mainloop()


You have three different ways of doing the same:

1) Use the built-in "command" configuration, like the one you use on buttons

import tkinter as tkfrom tkinter import messagebox as tk_messageboxdef spinbox1_callback():    tk_messagebox.showinfo("Spinbox callback", "You changed the spinbox.")if __name__ == "__main__":    master = tk.Tk()    spinbox1 = tk.Spinbox(master, from_=0, to=10, command=spinbox1_callback)    spinbox1.pack()    tk.mainloop()

2) Use the event bindings to capture specific events:http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

import tkinter as tkfrom tkinter import messagebox as tk_messageboxroot = tk.Tk()def callback(event):    tk_messagebox.showinfo("clicked at", event.x, event.y)frame = tk.Frame(root, width=100, height=100)frame.bind("<Button-1>", callback)frame.pack()root.mainloop()

3) "trace" changes on a tkinter variable classes, so if your widget uses a StringVar, BooleanVar, IntVar, or DoubleVar in the textvariable parameter, you will get a callback once it gets updated. https://effbot.org/tkinterbook/variable.htm

import tkinter as tkfrom tkinter import messagebox as tk_messageboxif __name__ == "__main__":    master = tk.Tk()    widget_contents = tk.StringVar()    widget_contents.set('')    some_entry = tk.Entry(master,textvariable=widget_contents,width=10)    some_entry.pack()    def entry1_callback(*args):        tk_messagebox.showinfo("entry callback", "You changed the entry %s" % str(args))        some_entry.update_idletasks()    widget_contents.trace('w',entry1_callback)    tk.mainloop()