Back to Python
2026-02-195 min read

Check Checkbox (Python Programming)

Learn Check Checkbox (Python Programming) step by step with clear examples and exercises.

Why This Matters

Learning to create and manage checkboxes in Python using Tkinter is crucial for building interactive and user-friendly applications. Checkboxes allow users to select multiple options from a list, making them essential GUI elements for various applications. In this lesson, we will guide you through the core concept, provide a worked example, common mistakes, practice questions, and frequently asked questions.

Prerequisites

Before diving into the core concept, make sure you have the following prerequisites:

  1. Basic understanding of Python programming concepts such as variables, functions, and control structures.
  2. Familiarity with Tkinter library for creating GUIs in Python. This includes knowledge of widgets, event handling, and layout management.
  3. Understanding of basic Tkinter syntax and common widget properties.

Core Concept

To create a checkbox in Tkinter, we use the Checkbutton widget. The Checkbutton widget is a graphical control that allows users to select or deselect an option by clicking on it. Here's an example demonstrating how to create a simple GUI with a single checkbox:

import tkinter as tk

root = tk.Tk()
check_var = tk.IntVar() # Variable to store the checkbox state

check_button = tk.Checkbutton(root, text="Check this box", variable=check_var)
check_button.pack()

def on_check_change():
print("Checkbox state:", check_var.get())

check_button.configure(command=on_check_change) # Bind the function to the check button
root.mainloop()

In this example, we create a Tkinter window (root) and an IntVar variable called check_var. The Checkbutton widget is created with the text "Check this box" and the variable check_var, which stores the checkbox state. We then define a function on_check_change() that prints the current state of the checkbox whenever it changes. Finally, we bind the function to the check button using the configure() method.

When you run this code, a window will appear with a single checkbox labeled "Check this box". Clicking on the checkbox will print its current state (1 for checked and 0 for unchecked) in the console.

Worked Example

Let's create a more complex GUI with multiple checkboxes and handle their states:

import tkinter as tk

root = tk.Tk()

Create variables for each checkbox

check_var1 = tk.IntVar()

check_var2 = tk.IntVar()

check_var3 = tk.IntVar()

Create checkboxes and pack them

check1 = tk.Checkbutton(root, text="Option 1", variable=check_var1)

check2 = tk.Checkbutton(root, text="Option 2", variable=check_var2)

check3 = tk.Checkbutton(root, text="Option 3", variable=check_var3)

check1.pack()

check2.pack()

check3.pack()

Define a function to print the selected checkboxes

def on_check_change():

print("Selected options:")

if check_var1.get():

print("- Option 1")

if check_var2.get():

print("- Option 2")

if check_var3.get():

print("- Option 3")

Bind the function to the check buttons

check1.configure(command=on_check_change)

check2.configure(command=on_check_change)

check3.configure(command=on_check_change)

root.mainloop()


In this example, we create three checkboxes and bind the `on_check_change()` function to each of them. The function prints the selected options whenever a checkbox changes its state. When you run this code, a window will appear with three checkboxes labeled "Option 1", "Option 2", and "Option 3". Clicking on any of the checkboxes will print the corresponding option in the console.

Common Mistakes

  1. Forgetting to import the Tkinter module:
from tkinter import * # Incorrect import statement

Instead, use:

import tkinter as tk
  1. Misunderstanding the purpose of IntVar and StringVar variables:
  • IntVar is used to store integer values (e.g., checkbox states)
  • StringVar is used to store string values (e.g., text entry contents)
  1. Not binding the event handler function to the check button:
check_button = tk.Checkbutton(root, text="Check this box", variable=check_var)
check_button.pack() # No command binding here!

Instead, use:

check_button = tk.Checkbutton(root, text="Check this box", variable=check_var)
check_button.configure(command=on_check_change)
check_button.pack()
  1. Using the wrong type of variable for a checkbox:
check_var = tk.StringVar() # Incorrect variable type for checkboxes

Instead, use:

check_var = tk.IntVar() # Correct variable type for checkboxes
  1. Creating a checkbox without specifying the variable option:
check_button = tk.Checkbutton(root, text="Check this box") # Missing variable attribute

Instead, use:

check_var = tk.IntVar()
check_button = tk.Checkbutton(root, text="Check this box", variable=check_var)

Practice Questions

  1. Create a GUI with two checkboxes: "Fruit" and "Vegetable". When both are checked, display the message "You have selected fruits and vegetables." Otherwise, display the sum of the selected items' count.
  2. Modify the worked example to include an "All" checkbox that selects or deselects all other checkboxes when clicked.
  3. Create a GUI with radio buttons instead of checkboxes and implement similar functionality as the worked example.
  4. Implement a checkbox that toggles the visibility of another widget (e.g., a label or an entry field) based on its state.
  5. Create a custom checkbox with a different appearance, such as changing the color or font of the text label associated with the checkbox.

FAQ

How can I create a disabled checkbox in Tkinter?

To create a disabled checkbox, set the state attribute to 'disabled' when creating the checkbutton:

check_button = tk.Checkbutton(root, text="Check this box", variable=check_var, state='disabled')

How can I change the appearance of a checkbox in Tkinter?

To customize the appearance of a checkbox, you can use the font and bg attributes to change the font and background color, respectively:

check_button = tk.Checkbutton(root, text="Check this box", variable=check_var, font=("Arial", 14), bg="#f0e68c")

How can I add a label next to the checkbox in Tkinter?

To add a label next to the checkbox, create the label and pack it before or after the checkbutton:

label = tk.Label(root, text="Select option:")
label.pack() # Pack the label before or after the checkbutton
check_button = tk.Checkbutton(root, text="Check this box", variable=check_var)
check_button.pack()
Check Checkbox (Python Programming) | Python | XQA Learn