Entry Widgets
Entry Widgets
Buttons and labels are useful, but real apps also need user input.
Users may need to type:
- names
- passwords
- numbers
- search text
- messages
Tkinter uses the Entry widget for single-line text input.
What Is an Entry Widget?
An Entry widget is a text box where the user can type.
Examples:
- login username
- email field
- calculator number input
- search bar
Create a Basic Entry
import tkinter as tk
window = tk.Tk()
entry = tk.Entry(window)
entry.pack()
window.mainloop()
Read Input with get()
Use .get() to read what the user typed.
import tkinter as tk
def show_text():
print(entry.get())
window = tk.Tk()
entry = tk.Entry(window)
entry.pack()
button = tk.Button(window, text="Show", command=show_text)
button.pack()
window.mainloop()
If user types:
Hello
Output:
Hello
Show Result in Label
import tkinter as tk
def display_name():
name = entry.get()
label.config(text="Hello " + name)
window = tk.Tk()
entry = tk.Entry(window)
entry.pack()
button = tk.Button(window, text="Submit", command=display_name)
button.pack()
label = tk.Label(window, text="")
label.pack()
window.mainloop()
Set Entry Width
entry = tk.Entry(window, width=30)
Default Text
Insert text into the box.
entry.insert(0, "Type here")
0 means start position.
Clear Entry
entry.delete(0, tk.END)
Deletes all text.
Password Input
Hide typed characters.
entry = tk.Entry(window, show="*")
Used for passwords.
Full Example: Login Style
import tkinter as tk
def login():
username = user_entry.get()
label.config(text="Welcome " + username)
window = tk.Tk()
user_entry = tk.Entry(window)
user_entry.pack()
button = tk.Button(window, text="Login", command=login)
button.pack()
label = tk.Label(window)
label.pack()
window.mainloop()
Common Beginner Errors
Forgetting .get()
This gives the widget, not the text:
print(entry)
Use:
print(entry.get())
Missing pack()
Widget may not appear.
Reading Before Typing
If empty, result is an empty string.
Code Along
Create:
- Entry box
- Button =
Show Name - Label result
When clicked, show typed name.
Mini Challenge
Build a mini calculator.
Widgets:
- Entry 1
- Entry 2
- Button = Add
- Label result
When clicked:
Add both numbers and show answer.
Example:
5 + 3 = 8
Real World Use Case
Entry widgets are used in login forms, search bars, calculators, settings forms, and registration apps.
Quiz
- What is an Entry widget?
- What does
.get()do? - How do you clear an Entry?
- How do you hide password text?
Assignment
Create a GUI that asks for first name and last name, then shows the full name when button is clicked.
Summary
You learned how to use Entry widgets to collect user input and use typed values in Tkinter apps.