Tkinter – Read input from user using Entry widget

Read input from user using Entry

To read input from user using Entry widget in Tkinter, define an Entry widget using Entry() class, and read the text using get() method on the Entry widget.

Code Snippet

The following is a simple skeletal code to define an Entry widget, and read the text from the Entry widget.

tkWindow = Tk()  
entry = Entry(tkWindow)


1. Read name from user

In the following program, we read user name from the user.

We display label, entry, and a button in grid view in the Tkinter window. When user clicks on the button, we call printDetails() method. In printDetails() method, we get the text from the entry widget, and print it to console.

Python Program

from tkinter import *
from functools import partial

def printDetails(usernameEntry) :
    usernameText = usernameEntry.get()
    print("user entered :", usernameText)

# Window
tkWindow = Tk()  
tkWindow.title('Python Examples')

# Label
usernameLabel = Label(tkWindow, text="Enter your name")

# Entry for user input
usernameEntry = Entry(tkWindow)

# Define callable function with printDetails function and usernameEntry argument
printDetailsCallable = partial(printDetails, usernameEntry)

# Submit button
submitButton = Button(tkWindow, text="Submit", command=printDetailsCallable)

# Place label, entry, and button in grid
usernameLabel.grid(row=0, column=0)
usernameEntry.grid(row=0, column=1) 
submitButton .grid(row=1, column=1)  

# Main loop


Tkinter – Read input from user using Entry widget
Tkinter – Read input from user using Entry widget

Related Tutorials

Code copied to clipboard successfully 👍