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


Callback functions in Tkinter are generally used to handle a specific event happening in a widget. We can add an event callback function to the Entry widget whenever it gets modified. We will create an event callback function by specifying the variable that stores the user input. By using the trace("mode", lambda variable, variable: callback()) method with the variable, we can trace the input on the Label widget in the window.

Example

#Import the Tkinter library
from tkinter import *
#Create an instance of Tkinter frame
win= Tk()
#Define the geometry
win.geometry("750x250")
def callback(var):
   content= var.get()
   Label(win, text=content).pack()
#Create an variable to store the user-input
var = StringVar()
var.trace("w", lambda name, index,mode, var=var: callback(var))
#Create an Entry widget
e = Entry(win, textvariable=var)
e.pack()
win.mainloop()

Output

Running the above code will print the input character of the Entry widget on the text Label. Now, type something on the given Entry widget to echo the input event on the Label widget.

Updated on: 21-Apr-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements