How To Disable Checkbutton Tkinter?
Last Updated :
29 May, 2024
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 ProgramaticallyConclusion
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.
Similar Reads
How To Disable / Enable A Button In Tkinter?
Disabling and enabling buttons in Tkinter is a fundamental aspect of creating interactive and user-friendly GUI applications. While the basic method involves changing the button's state property, there are various ways to achieve this based on different use cases and requirements. In this article, w
2 min read
Python Tkinter - Checkbutton Widget
The Checkbutton widget is a standard Tkinter widget that is used to implement on/off selections. Checkbuttons can contain text or images. When the button is pressed, Tkinter calls that function or method. Note:Â For more reference, you can read our article, What is WidgetsPython Tkinter OverviewPytho
5 min read
How to Change Tkinter Button State?
Tkinter is a Python Package for creating GUI applications. Python has a lot of GUI frameworks, but Tkinter is the only framework thatâs built into the Python standard library. Tkinter has several strengths; itâs cross-platform, so the same code works on Windows, macOS, and Linux. Tkinter is lightwei
4 min read
How to move a Tkinter button?
Prerequisite: Creating a button in tkinter Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. As Tk and Tkinter are available on most of the Unix platforms as well as on the W
4 min read
How to clear Tkinter Canvas?
Tkinter is a Python Package for creating effective GUI applications. Tkinter's Canvas widget is nothing but a rectangular area that is used for drawing pictures, simple shapes, or any complex graph. We can place any widgets like text, button, or frames on the canvas. The task here is to generate a P
2 min read
Python | How to dynamically change text of Checkbutton
Tkinter is a GUI (Graphical User interface) module which is used to create various types of applications. It comes along with the Python and consists of various types of widgets which can be used to make GUI more attractive and user-friendly. Checkbutton is one of the widgets which is used to select
2 min read
Disable Kivy Button
In this article, we will learn how to disable a button in kivy, there are some places where we need to disable the buttons So in this article you will learn how to do that. Kivy Tutorial â Learn Kivy with Examples. The Button is a Label with associated actions that are triggered when the button is p
3 min read
How to Disable an Entry Widget in Tkinter?
In Tkinter, Entry widgets serve as a fundamental component for user input. However, there are instances in GUI applications where you may want to disable an Entry widget to prevent users from altering its contents. This article explores three distinct approaches to disable Entry widgets in Tkinter,
2 min read
Python Tkinter - Create Button Widget
The Tkinter Button widget is a graphical control element used in Python's Tkinter library to create clickable buttons in a graphical user interface (GUI). It provides a way for users to trigger actions or events when clicked.Note:Â For more reference, you can read our article:What is WidgetsPython Tk
6 min read
How to make Rounded buttons in Tkinter
Tkinter is a Python module that is used to create GUI (Graphical User Interface) applications with the help of a variety of widgets and functions. Like any other GUI module, it also supports images i.e you can use images in the application to make it more attractive. In this article, we will discuss
2 min read