Cómo cambiar el estado del Botón Tkinter
Botón Tkinter tiene dos estados,
NORMAL- El botón puede ser pulsado por el usuario.DISABLE- El botón no es clicable
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.geometry("300x100")
button1 = tk.Button(app, text="Button 1", state=tk.DISABLED)
button2 = tk.Button(app, text="EN/DISABLE Button 1")
button1.pack(side=tk.LEFT)
button2.pack(side=tk.RIGHT)
app.mainloop()
El botón izquierdo está deshabilitado (grisáceo) y el botón derecho es normal.

Los estados pueden ser modificados en el método de diccionario o en el método de configuración.
try:
import Tkinter as tk
except:
import tkinter as tk
def switchButtonState():
if button1["state"] == tk.NORMAL:
button1["state"] = tk.DISABLED
else:
button1["state"] = tk.NORMAL
app = tk.Tk()
app.geometry("300x100")
button1 = tk.Button(app, text="Python Button 1", state=tk.DISABLED)
button2 = tk.Button(app, text="EN/DISABLE Button 1", command=switchButtonState)
button1.pack(side=tk.LEFT)
button2.pack(side=tk.RIGHT)
app.mainloop()
Haciendo clic en button2, llama a la función switchButtonState para cambiar el estado de button1 de DISABLED a NORMAL, o viceversa.

El status es la opción del widget Button de Tkinter. Todas las opciones del widget Button son las claves de Button como un diccionario.
def switchButtonState():
if button1["state"] == tk.NORMAL:
button1["state"] = tk.DISABLED
else:
button1["state"] = tk.NORMAL
El status se actualiza cambiando el valor de status en el diccionario Button.
El status también puede ser modificado usando el método config del objeto Button. Por lo tanto, la función switchButtonState() también puede ser implementada como se muestra a continuación,
def switchButtonState():
if button1["state"] == tk.NORMAL:
button1.config(state=tk.DISABLED)
else:
button1.config(state=tk.NORMAL)
Incluso las cadenas normal y disabled podrían ser usadas simplemente en lugar de tk.NORMAL y tk.DISABLED.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook