The Wayback Machine - https://web.archive.org/web/20241001164815/https://www.geeksforgeeks.org/python-tkinter-checkbutton-widget/
Open In App

Python Tkinter – Checkbutton Widget

Last Updated : 01 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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,

  1. What is Widgets
  2. Python Tkinter Overview
  3. Python Tkinter Tutorial

Tkinter Checkbutton Widget Syntax

The syntax to use the checkbutton is given below.

Syntax: Checkbutton ( master, options)
Parameters:

  • master: This parameter is used to represents the parent window.
  • options:There are many options which are available and they can be used as key-value pairs separated by commas.

Tkinter Checkbutton Options

The following are commonly used Options that can be used with this widget:

  • activebackground: This option used to represent the background color when the checkbutton is under the cursor.
  • activeforeground: This option used to represent the foreground color when the checkbutton is under the cursor.
  • bg: This option used to represent the normal background color displayed behind the label and indicator.
  • bitmap: This option used to display a monochrome image on a button.
  • bd: This option used to represent the size of the border around the indicator and the default value is 2 pixels.
  • command: This option is associated with a function to be called when the state of the checkbutton is changed.
  • cursor: By using this option, the mouse cursor will change to that pattern when it is over the checkbutton.
  • disabledforeground: The foreground color used to render the text of a disabled checkbutton. The default is a stippled version of the default foreground color.
  • font: This option used to represent the font used for the text.
  • fg: This option used to represent the color used to render the text.
  • height: This option used to represent the number of lines of text on the checkbutton and it’s default value is 1.
  • highlightcolor: This option used to represent the color of the focus highlight when the checkbutton has the focus.
  • image: This option used to display a graphic image on the button.
  • justify: This option used to control how the text is justified: CENTER, LEFT, or RIGHT.
  • offvalue: The associated control variable is set to 0 by default if the button is unchecked. We can change the state of an unchecked variable to some other one.
  • onvalue: The associated control variable is set to 1 by default if the button is checked. We can change the state of the checked variable to some other one.
  • padx: This option used to represent how much space to leave to the left and right of the checkbutton and text. It’s default value is 1 pixel.
  • pady: This option used to represent how much space to leave above and below the checkbutton and text. It’s default value is 1 pixel.
  • relief: The type of the border of the checkbutton. It’s default value is set to FLAT.
  • selectcolor: This option used to represent the color of the checkbutton when it is set. The Default is selectcolor=”red”.
  • selectimage: The image is shown on the checkbutton when it is set.
  • state: It represents the state of the checkbutton. 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.
  • text: This option used use newlines (“\n”) to display multiple lines of text.
  • underline: This option used to represent the index of the character in the text which is to be underlined. The indexing starts with zero in the text.
  • variable: This option used to represents the associated variable that tracks the state of the checkbutton.
  • width: This option used to represents the width of the checkbutton. and also represented in the number of characters that are represented in the form of texts.
  • wraplength: This option will be broken text into the number of pieces.

Methods

Methods used in this widgets are as follows:

  • deselect(): This method is called to turn off the checkbutton.
  • flash(): The checkbutton is flashed between the active and normal colors.
  • invoke(): This method will invoke the method associated with the checkbutton.
  • select(): This method is called to turn on the checkbutton.
  • toggle(): This method is used to toggle between the different Checkbuttons.

Checkbutton with Toggle Functionality

In this example, below code sets up a Tkinter window with a checkbutton labeled “Enable Feature”. When clicked, it prints whether the button is selected or deselected. It customizes the appearance of the checkbutton and adds a bitmap image to it. Finally, the checkbutton is displayed in the window and flashes briefly.

Python3
import tkinter as tk

def on_button_toggle():
    if var.get() == 1:
        print("Checkbutton is selected")
    else:
        print("Checkbutton is deselected")

root = tk.Tk()

# Creating a Checkbutton
var = tk.IntVar()
checkbutton = tk.Checkbutton(root, text="Enable Feature", variable=var, 
                             onvalue=1, offvalue=0, command=on_button_toggle)

# Setting options for the Checkbutton
checkbutton.config(bg="lightgrey", fg="blue", font=("Arial", 12), 
                   selectcolor="green", relief="raised", padx=10, pady=5)

# Adding a bitmap to the Checkbutton
checkbutton.config(bitmap="info", width=20, height=2)

# Placing the Checkbutton in the window
checkbutton.pack(padx=40, pady=40)

# Calling methods on the Checkbutton
checkbutton.flash()

root.mainloop()

Output

ko

Python Tkinter – Checkbutton Widget

Multiple Checkbuttons for Selection

In this example, below code creates a Tkinter window with three checkbuttons labeled “Tutorial”, “Student”, and “Courses”. Each button toggles between selected and deselected states. The window size is set to 300×200 pixels, and each button has a size of 2×10 units.

Python3
from tkinter import *

root = Tk() 
root.geometry("300x200") 

w = Label(root, text ='GeeksForGeeks', font = "50") 
w.pack() 

Checkbutton1 = IntVar() 
Checkbutton2 = IntVar() 
Checkbutton3 = IntVar() 

Button1 = Checkbutton(root, text = "Tutorial", 
                    variable = Checkbutton1, 
                    onvalue = 1, 
                    offvalue = 0, 
                    height = 2, 
                    width = 10) 

Button2 = Checkbutton(root, text = "Student", 
                    variable = Checkbutton2, 
                    onvalue = 1, 
                    offvalue = 0, 
                    height = 2, 
                    width = 10) 

Button3 = Checkbutton(root, text = "Courses", 
                    variable = Checkbutton3, 
                    onvalue = 1, 
                    offvalue = 0, 
                    height = 2, 
                    width = 10) 
    
Button1.pack() 
Button2.pack() 
Button3.pack() 

mainloop() 

Output



Previous Article
Next Article

Similar Reads

Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
Tkinter is a GUI (Graphical User Interface) module which comes along with the Python itself. This module is widely used to create GUI applications. tkinter.ttk is used to create the GUI applications with the effects of modern graphics which cannot be achieved using only tkinter. Checkbutton is used to select multiple options. Checkbuttons can be cr
2 min read
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 TkinterThe checkbutton can be
3 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 multiple options. Checkbutton can be created as f
2 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Python | Menu widget in Tkinter
Tkinter is Python’s standard GUI (Graphical User Interface) package. It is one of the most commonly used package for GUI applications which comes with the Python itself. Menus are the important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipu
2 min read
Progressbar widget in Tkinter | Python
The purpose of this widget is to reassure the user that something is happening. It can operate in one of two modes - In determinate mode, the widget shows an indicator that moves from beginning to end under program control. In indeterminate mode, the widget is animated so the user will believe that something is in progress. In this mode, the indica
2 min read
Python | PanedWindow Widget in Tkinter
Tkinter supports a variety of widgets to make GUI more and more attractive and functional. The PanedWindow widget is a geometry manager widget, which can contain one or more child widgets panes. The child widgets can be resized by the user, by moving separator lines sashes using the mouse. Syntax: PanedWindow(master, **options) Parameters: master:
3 min read
Python Tkinter - Toplevel Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in Python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter Toplevel widget A Toplevel widget is used to create a window on top of all other w
3 min read
Combobox Widget in tkinter | Python
Python provides a variety of GUI (Graphic User Interface) types such as PyQT, Tkinter, Kivy, WxPython, and PySide. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to understand. The word Tkinter comes from the Tk interface. The tkinter module is available in Python standard library which has to be impo
3 min read
Python Tkinter - ListBox Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter ListBox widget The ListBox widget is used to display different types of items. The
2 min read
Python Tkinter - Scale Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter Scale widget The Scale widget is used whenever we want to select a specific value
2 min read
Python Tkinter - Canvas Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs.Note: For more information, refer to Python GUI – tkinter Canvas widget The Canvas widget lets us display various graphics on the application
3 min read
Python Tkinter - MessageBox Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy ta
2 min read
Python Tkinter - Menubutton Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy ta
4 min read
Creating Tabbed Widget With Python-Tkinter
Python offers a range of GUI frameworks that can be used to develop GUI based applications in Python. The most widely used Python interface is Tk interface or tkinter( as renamed in Python 3.x) . The Tkinter module offers a wide range of widgets that can be used to develop GUI applications much faster and easier compared to the other interfaces off
5 min read
Python Tkinter - Validating Entry Widget
Python offers a variety of frameworks to work with GUI applications. Tkinter or Tk interface is one of the most widely used Python interface to build GUI based applications. There are applications that require validation of text fields to prevent invalid input from the user before the form is submitted. Python allows input validation by allowing va
5 min read
Python Tkinter - ScrolledText Widget
Tkinter is a built-in standard python library. With the help of Tkinter, many GUI applications can be created easily. There are various types of widgets available in Tkinter such as button, frame, label, menu, scrolledtext, canvas and many more. A widget is an element that provides various controls. ScrolledText widget is a text widget with a scrol
3 min read
Python Tkinter – How to display a table editor in a text widget?
In this article, we will discuss how to display a table editor in a text widget. Before moving ahead let's create a simple text widget first with the help of Tkinter. For the link to the Excel file used in the code click here. Text Widget The Text widget is used to show the text editor in the Tkinter window. A user can modify the text in the text e
3 min read
Python Tkinter - Frame Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.
3 min read
Hide Python Tkinter Text widget border
Tkinter is a standard GUI library for Python, providing tools to create graphical user interfaces easily. When designing interfaces, developers often encounter the need to customize widget appearances, including removing widget borders. Borders can sometimes clash with the desired aesthetic or layout of the application. This article explores what w
3 min read
Validating Entry Widget In Python Tkinter
When developing graphical user interfaces (GUIs) in Python, Tkinter is a powerful and widely-used toolkit that simplifies the process. One of the common tasks in GUI applications is obtaining user input through form fields. Tkinter's Entry widget is designed for this purpose, allowing users to input text. However, ensuring the correctness of the en
7 min read
Access the Actual Tab Widget of ttk.Notebook in Python Tkinter
Tab widgets of ttk.Notebook are essential components in a Tkinter GUI for organizing content into multiple tabs. Accessing these tab widgets allows for customization and manipulation of their properties. In this article, we will explore two different approaches to accessing the actual tab widget of ttk.Notebook. Access the actual tab widget of ttk.
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 Tkinter OverviewPython Tkinter TutorialTkinter But
6 min read
Python Tkinter - Text Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs.Note: For more information, refer to Python GUI – tkinter  Text WidgetText Widget is used where a user wants to insert multiline text fields.
5 min read
Python Tkinter - Entry Widget
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.In Python3 Tkinter is come preinstalled But you can also install it by using
5 min read
Tkinter | Adding style to the input text using ttk.Entry widget
Tkinter is a GUI (Graphical User Interface) module which is widely used to create GUI applications. It comes along with the Python itself. Entry widgets are used to get the entry from the user. It can be created as follows- entry = ttk.Entry(master, option = value, ...) Code #1: Creating Entry widget and taking input from user (taking only String d
2 min read
How to change background color of Tkinter OptionMenu widget?
Prerequisites: Tkinter While creating GUI applications, there occur various instances in which you need to make selections among various options available. For making such choices, the Option Menu widget was introduced. In this article, we will be discussing the procedure of changing menu background color of Tkinter’s option Menu widget. To achieve
2 min read
Tkinter - Read only Entry Widget
Python has a number of frameworks to develop GUI applications like PyQT, Kivy, Jython, WxPython, PyGUI, and Tkinter. Python tkinter module offers a variety of options to develop GUI based applications. Tkinter is an open-source and available under Python license. Tkinter provides the simplest and fastest way to develop a GUI application. Tkinter su
4 min read
Create Find and Replace features in Tkinter Text Widget
Prerequisites: Python GUI – tkinterFirstly we need to find all the words or letters that we want to replace in our below text for that we will use the find function in which we will determine all the starting and the ending indexes from the editor of the same word after that we will apply the replace functionality in which we will delete that part
4 min read
Change the color of certain words in the tkinter text widget
Python has various options for Graphical User Interface (GUI) development. One of the options is Tkinter. Tkinter and Python together provide a faster way for GUI development. The Tk GUI toolkit provides an object-oriented interface. For creating GUI applications using Tkinter we have to follow a few steps – Import Tkinter module.Create the main wi
3 min read
Practice Tags :