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()
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.