CodingNic

GUI Development

Entry Widgets

GUI Development 40 min read

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

python
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.

python
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:

text
Hello

Output:

text
Hello

Show Result in Label

python
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

python
entry = tk.Entry(window, width=30)

Default Text

Insert text into the box.

python
entry.insert(0, "Type here")

0 means start position.


Clear Entry

python
entry.delete(0, tk.END)

Deletes all text.


Password Input

Hide typed characters.

python
entry = tk.Entry(window, show="*")

Used for passwords.


Full Example: Login Style

python
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:

python
print(entry)

Use:

python
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:

text
5 + 3 = 8

Real World Use Case

Entry widgets are used in login forms, search bars, calculators, settings forms, and registration apps.


Quiz

  1. What is an Entry widget?
  2. What does .get() do?
  3. How do you clear an Entry?
  4. 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.