Python – How to disable (grey out) a Checkbutton in Tkinter

buttonpythontkinteruser interface

I can't figure out how to grey out a Tkinter Checkbutton.

I tried using state=DISABLED but it didn't work and I got an error saying

_tkinter.TclError: bad option "-enable": must be -column, -columnspan, -in, -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky

How can I temporarily disable a Checkbutton?

Best Answer

Using state=DISABLED is the correct way to do this.

However, you must be putting it in the wrong place. state is an option of Checkbutton, so it needs to be used like this:

Checkbutton(state=DISABLED)

Below is a sample script to demonstrate:

from Tkinter import Tk, Checkbutton, DISABLED
root = Tk()
check = Checkbutton(text="Click Me", state=DISABLED)
check.grid()
root.mainloop()

If you want to change a checkbutton's state programmatically, use Tkinter.Checkbutton.config.

Below is a sample script to demonstrate:

from Tkinter import Tk, Checkbutton, DISABLED
root = Tk()
def click():
    check.config(state=DISABLED)
check = Checkbutton(text="Click Me", command=click)
check.grid()
root.mainloop()