Tkinter: How to change state to disable/enable Button or other widget
You can deactivate button with
button.config(state="disabled")
and activate it back with
button.config(state="normal")
eventually
button.config(state="active")
The same way you can also deactivate other widgets - ie. Label
, Entry
, etc.
Other widgets may have different states - ie. Entry
has state "readonly"
(but it doesn't have "active"
)
Minimal working example
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions ---
def on_press_1():
b1.config(state="disabled")
l1.config(state="disabled")
e1.config(state="disabled") # or `readonly`
def on_press_2():
b1.config(state="normal") # or `active`
l1.config(state="normal") # or `active`
e1.config(state="normal")
# --- main ---
root = tk.Tk()
l1 = tk.Label(root, text="Label Text")
l1.pack()
e1 = tk.Entry(root)
e1.insert(0, "Entry Text")
e1.pack()
b1 = tk.Button(root, text="Deactivate", command=on_press_1)
b1.pack()
b2 = tk.Button(root, text="Activate", command=on_press_2)
b2.pack()
root.mainloop()
If you like it
Buy a Coffee
