Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to disable an Entry widget in Tkinter?
Tkinter Entry widget accepts single line user-input in an entry field. You can customize the width, background color and size of the entry widget based on the need of your application.
To disable an Entry widget, you can use the state='disabled' property either in the constructor or by calling the config() method. Disabling the Entry widget prevents users from editing or adding values to it, and visually greys out the widget.
Method 1: Disabling During Creation
You can disable an Entry widget when creating it by setting state='disabled' in the constructor ?
import tkinter as tk
# Create main window
root = tk.Tk()
root.geometry("400x200")
root.title("Disabled Entry Example")
# Create a disabled entry widget
disabled_entry = tk.Entry(root, width=30, state='disabled', font=('Arial', 12))
disabled_entry.pack(pady=20)
# Create a normal entry for comparison
normal_entry = tk.Entry(root, width=30, font=('Arial', 12))
normal_entry.pack(pady=10)
# Add labels
tk.Label(root, text="Disabled Entry (above)", font=('Arial', 10)).pack()
tk.Label(root, text="Normal Entry (above)", font=('Arial', 10)).pack()
root.mainloop()
Method 2: Disabling After Creation
You can dynamically enable or disable an Entry widget using the config() method ?
import tkinter as tk
# Create main window
root = tk.Tk()
root.geometry("400x250")
root.title("Dynamic Entry Control")
def toggle_entry():
current_state = entry.cget('state')
if current_state == 'normal':
entry.config(state='disabled')
button.config(text='Enable Entry')
else:
entry.config(state='normal')
button.config(text='Disable Entry')
# Create entry widget
entry = tk.Entry(root, width=30, font=('Arial', 12))
entry.pack(pady=20)
entry.insert(0, "Type something here...")
# Create toggle button
button = tk.Button(root, text='Disable Entry', font=('Arial', 12), command=toggle_entry)
button.pack(pady=10)
root.mainloop()
Entry Widget States
The Entry widget supports three main states ?
| State | Description | User Interaction |
|---|---|---|
'normal' |
Default state | Can edit text |
'disabled' |
Greyed out appearance | Cannot edit text |
'readonly' |
Normal appearance | Can select but not edit |
Complete Example with Multiple States
Here's a comprehensive example showing all three states ?
import tkinter as tk
root = tk.Tk()
root.geometry("500x300")
root.title("Entry Widget States")
def set_state(state):
entry.config(state=state)
status_label.config(text=f"Current state: {state}")
# Create entry widget
entry = tk.Entry(root, width=40, font=('Arial', 12))
entry.pack(pady=20)
entry.insert(0, "Sample text - try editing!")
# Create control buttons
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
tk.Button(button_frame, text='Normal', command=lambda: set_state('normal')).pack(side='left', padx=5)
tk.Button(button_frame, text='Disabled', command=lambda: set_state('disabled')).pack(side='left', padx=5)
tk.Button(button_frame, text='Readonly', command=lambda: set_state('readonly')).pack(side='left', padx=5)
# Status label
status_label = tk.Label(root, text="Current state: normal", font=('Arial', 10))
status_label.pack(pady=10)
root.mainloop()
Conclusion
Use state='disabled' to prevent user input in Entry widgets. You can set this during creation or change it dynamically using config(). The 'readonly' state allows selection but prevents editing.
