How to connect a variable to the Tkinter Entry widget?



Tkinter Entry widget is an Input widget that supports and accepts single-line user input. It accepts all types of characters in UTF-8 module. In order to get the input from the Entry widget, we have to define a variable (based on Data Types it accepts) that accepts only string characters. Then, by using get() method, we can print the given input from the Entry widget.

Example

# Import the Tkinter Library from tkinter import * # Create an instance of Tkinter Frame win = Tk() # Set the geometry of window win.geometry("700x250") # Define a String Variable var = StringVar() # Define a function to print the Entry widget Input def printinput(*args):    print(var.get()) # Create an Entry widget entry = Entry(win, width=35, textvariable=var) entry.pack() # Trace the Input from Entry widget var.trace("w", printinput) win.mainloop()

Output

Running the above code will display a window with an Entry widget.

When we write something in the Entry widget, it will just print out all the characters from the Entry widget on the console.

H He Hel Hell Hello Hello Hello W Hello Wo Hello Wor Hello Worl Hello World Hello World!
Updated on: 2021-05-26T11:59:24+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements