How do I get an event callback when a Tkinter Entry widget is modified?

bfops picture bfops · Jul 1, 2011 · Viewed 67.6k times · Source

Exactly as the question says. Text widgets have the <<Modified>> event, but Entry widgets don't appear to.

Answer

Steven Rumbalski picture Steven Rumbalski · Jul 1, 2011

Add a Tkinter StringVar to your Entry widget. Bind your callback to the StringVar using the trace method.

from Tkinter import *

def callback(sv):
    print sv.get()

root = Tk()
sv = StringVar()
sv.trace("w", lambda name, index, mode, sv=sv: callback(sv))
e = Entry(root, textvariable=sv)
e.pack()
root.mainloop()