Taking input from the user in Tkinter

There might be times when we need to take the user input in our Tkinter application. We can get the user Input in a Single Line text input through the Entry widget using get() method. To display the Captured input, we can either print the message on the screen or display the input with the help of the Label widget.


#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

def display_text():
   global entry
   string= entry.get()

#Initialize a Label to display the User Input
label=Label(win, text="", font=("Courier 22 bold"))

#Create an Entry widget to accept User Input
entry= Entry(win, width= 40)

#Create a Button to validate Entry Widget
ttk.Button(win, text= "Okay",width= 20, command= display_text).pack(pady=20)



Running the above code will display a window that will contain an entry widget that accepts the single line User Input.

Now, write some text in the given entry widget and press "Okay" to validate and display the entry widget on the screen.