Python Tkinter - Entry Widget
The Entry widget in Python's Tkinter module is used to accept single-line text input from the user. It's commonly used in forms, login interfaces, and other applications where textual input is required.
Syntax
entry = tk.Entry( root, options,... )
Common Options
Option | Description |
---|---|
width=30 | Width in characters |
font=("Helvetica",14) | The font and size used for the text |
fg='gray' | Text color |
bg='lightyellow' | Background color |
bd=3 | Border thickness |
relief='sunken' | Border style |
justify='left' | left,center,or right |
show=' ' | Masks input (e.g., for passwords: show="*" ) |
textvariable=text_var | Link to StringVar |
validate='key' | Validate on every keystroke |
validatecommand=validate_func | Validation function |
The following example shows, how to create an Entry widget, retrieve the text entered by the user, and clear the Entry.
Example
import tkinter as tk from tkinter import * def get_entry(): entered_text = var.get() print("Entered text : ", entered_text) def clear_entry(): var.set("") #or var.set("New Text") root = tk.Tk() root.title("Entry Widget Example") #Create Tkinter variable and linked to Entry widgets var = tk.StringVar() # Create an Entry widget entry = tk.Entry(root, textvariable = var) entry.pack(padx=10,pady=10,side = LEFT) #disable the entry #entry.config(state='disabled') #make it readonly #entry.config(state='readonly') # Create a button to get the Entry text button = tk.Button(root, text="Show Entry", command=get_entry) button.pack(padx=10,pady=10,side = LEFT) # Create a button to Clear the Entry text button = tk.Button(root, text="Clear", command=clear_entry) button.pack(padx=10,pady=10,side = LEFT) root.mainloop()
Output:

Example 2: Events
In Tkinter, you can bind key events to widgets (like Entry, Text, or the main Tk window) to respond when a user presses a key.
Example
import tkinter as tk root = tk.Tk() # Validation function: allows only digits or empty string def on_focus_in(event): print("Entry is focused") def on_focus_out(event): print("Entry lost focus") def on_key_release(event): print("Key Released") def on_key_press(event): print(f"Key pressed: {event.keysym}") # Create Entry widget with validation entry = tk.Entry(root) entry.pack(pady=10) entry.bind("<FocusIn>", on_focus_in) entry.bind("<FocusOut>", on_focus_out) # Bind the key release event to the Entry entry.bind("<KeyRelease>", on_key_release) # Bind key press to Entry entry.bind("<KeyPress>", on_key_press) root.mainloop()
Entry Validation
The following example shows, an Entry widget that only allows numeric input.
Example
import tkinter as tk root = tk.Tk() # Validation function: allows only digits or empty string def only_numbers(value): return value.isdigit() or value == "" # Register the validation command (%P - New proposed value of the Entry) val_cmd = (root.register(only_numbers), "%P") # Create Entry widget with validation entry = tk.Entry(root, validate="key", validatecommand=val_cmd) entry.pack(pady=10) root.mainloop()