Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

tkinter

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

like image 363
bfops Avatar asked Jul 01 '11 13:07

bfops


People also ask

What is callback in Tkinter?

In Tkinter, some widgets allow you to associate a callback function with an event using the command binding. It means that you can assign the name of a function to the command option of the widget so that when the event occurs on the widget, the function will be called automatically.

What does Focus_set () do in Tkinter?

Sometimes, we need to change or modify the focus of any widget in the application which can be achieved by using the focus_set() method. This method sets the default focus for any widget and makes it active till the execution of the program.

What does get () do in Tkinter?

An Entry widget in Tkinter is nothing but an input widget that accepts single-line user input in a text field. To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.

Which one of the following operation that we can perform with entry widget?

Methods: The various methods provided by the entry widget are: get() : Returns the entry's current text as a string. delete() : Deletes characters from the widget. insert ( index, 'name') : Inserts string 'name' before the character at the given index.


1 Answers

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()   
like image 105
Steven Rumbalski Avatar answered Sep 17 '22 21:09

Steven Rumbalski