Get contents of a Tkinter Entry widget

TkinterServer Side ProgrammingProgramming

An Entry widget is a basic one-line character widget that supports only single-line text input. An Entry widget can be defined by initializing the Entry(parent, width) constructor.

To validate the Entry widget, we can use the get() method which results in the character entered in the Entry widget.

Let us define an Entry widget that accepts single-line text input, and we will print the character that is input in the Entry widget.

Example

#Import the required Libraries
from tkinter import *
from tkinter import ttk

#Create an instance of Tkinter frame
win = Tk()

#Set the geometry of Tkinter frame
win.geometry("750x250")
def get_content():
   #Get the content of Entry Widget
   print(entry.get())

#Create an entry widget
entry= Entry(win, width= 40)
entry.pack(pady= 20)

#Create a button to validate the entry widget
button= ttk.Button(win, text= "Get Content", command= get_content)
button.pack(pady=10)

win.mainloop()

Output

Running the above code will display a window that will contain an Entry widget and a button to get the content of the Entry widget.

Now click the "Get Content" Button to print the Content of Entry Widget. Once we click the Button, it will print the output as,

Hello World!
raja
Published on 03-May-2021 08:53:18
Advertisements