How To Disable Checkbutton Tkinter?

Tkinter is a module in Python which is used to create GUI applications. It has many useful widgets such as Label, Button, Radiobutton, Checkbutton, Listbox, and more. In this article, we will learn about the Checkbutton widget in Python and how we can disable it so that it is uneditable.

Steps to Disable Checkbutton in Tkinter

The checkbutton can be disabled by changing its ‘state’ option to DISABLED. When declaring the Checkbutton itself, it can be disabled using the option ‘state’. Otherwise, in another way, the checkbutton can be disabled later programmatically.

Let us see step by step how we can create and then disable a Checkbutton in Tkinter.

Import Tkinter Module

First, you need to import the Tkinter module. In Python 3.x, the module is imported as tkinter.

import tkinter as tk

Create the Main Application Window

Next, create the main application window using tk.Tk() class. It is the parent window which will contain all other tkinter widgets. Here we provide a title and dimensions to the Tkinter window.

root = tk.Tk()
root.title("Disable Checkbutton Widget Example")
root.geometry("400x300")

Create the Checkbutton Widget

Next, create the Checkbutton widget by calling the Checkbutton class. By default, the Checkbuttons are unchecked.

chkbtn = tk.Checkbutton(root, text="text_to_display")
chkbtn.pack()

Disable the Text Widget to Make It Read-Only

The simplest way to disable the Checkbutton while creating is by passing a third parameter to the Checkbutton() function. This third parameter is the known state. By default, it is set to normal. We can change it to DISABLED to make the checkbutton unresponsive. The state of the checkbutton is ACTIVE when it is under focus.

chkbtn = tk.Checkbutton(root, text="text_to_display", state=tk.DISABLED)

Another way to disable checkbutton is after creation. This can be done bycheck button using the Tkinter config() function and passing the state parameter to it which has the value – DISABLED.

chkbtn.config(state=tk.DISABLED)

Run the Main Event Loop

Finally, run the main event loop to display the window and make the application responsive to user interactions.

root.mainloop()

Code Implementation to Disable Checkbutton in Tkinter

Now, that we know how it works, let us see different code examples.

Disabling Checkbutton during Creation

We can disable the Checkbutton during its creation. This is a simple and easy way. You just need to pass a third parameter to the Checkbutton() function, which is its state. The state parameter when disabled, the user cannot check or uncheck that chebutton.

Python
# import tkinter module
import tkinter as tk

# Create the main application window
root = tk.Tk()
root.geometry("400x200")
root.title("Disable Checkbutton Widget Example")

#  Create the CheckButton widget
chkbtn1 = tk.Checkbutton(root,text="Python")
chkbtn2 = tk.Checkbutton(root,text="Java")

# checkbutton disabled on creating.
chkbtn3 = tk.Checkbutton(root,text="C++", state=tk.DISABLED)

chkbtn1.pack()
chkbtn2.pack()
chkbtn3.pack()

# Run the main event loop
root.mainloop()

Output:

Disable CheckButton while Creation


Disable Checkbutton Programmatically

Once the checkbuttons are created, they can be disabled programmatically later using the config() method and assigning DISABLED constant value to the option ‘state’.

Python
# import tkinter module
import tkinter as tk

# Function to disable the checkbutton 'chkbtn3' on clicking the button
def disable_checkbutton():
    # The checkbutton 'chkbtn3' is disabled now
    chkbtn3.config(state=tk.DISABLED)

# Create the main application window
root = tk.Tk()
root.geometry("300x150")
root.title("Disable Checkbutton Widget Example")

#  Create the CheckButton widget
chkbtn1 = tk.Checkbutton(root,text="Python")
chkbtn2 = tk.Checkbutton(root,text="Java")
chkbtn3 = tk.Checkbutton(root,text="C++")

chkbtn1.pack()
chkbtn2.pack()
chkbtn3.pack()

# Button to disable checkbutton on clicking this button
disable_Button = tk.Button(root,text="Disable",command=disable_checkbutton)
disable_Button.pack()

# Run the main event loop
root.mainloop()

Output:

Disable Checkbutton Programatically

Conclusion

In this article, we have discussed two different ways to disable Checkbutton in Tkinter. It can either be created as a disabled checkbutton, or it can be created as active Checkbutton and later it can be disabled.