SQL Tutorial

Python Tkinter Checkbutton – A Complete Guide

Python Tkinter CheckbuttonPython Tkinter Checkbutton
Python Tkinter Checkbutton

Python Tkinter Checkbutton

In GUI applications, collecting user input is an important part of creating interactive interfaces. Python’s standard GUI toolkit, Tkinter, provides several widgets for handling user interactions, and the Checkbutton is one of the most useful options when users need to make multiple selections.

A Checkbutton works like an on/off switch, allowing users to check or uncheck individual choices. It is commonly useful in forms, settings screens, and applications where users need to select their preferences.

Python Tkinter Checkbutton - A Complete Guide

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

What is a Tkinter Checkbutton?

A Checkbutton is a Tkinter widget that allows users to select multiple choices from a list of available options. Each Checkbutton can be switched between a selected and unselected state, making it suitable for applications that require multiple-choice input.

The Checkbutton is also flexible because it can display both text and images, allowing developers to customize the appearance of their graphical interfaces.

Syntax of Checkbutton

w = Checkbutton(master, options)

Here, master represents the parent widget, such as a Tkinter window or frame. The options parameter contains the different configuration attributes used to customize the Checkbutton.

Tkinter Checkbutton Options

The following table lists the commonly available configuration options for the Checkbutton widget:

SNOptionDescription
1activebackgroundSets the background color when the Checkbutton is hovered.
2activeforegroundSets the foreground or text color when the Checkbutton is hovered.
3bgDefines the background color of the Checkbutton.
4bitmapDisplays a monochrome image on the Checkbutton.
5bdSpecifies the border size of the Checkbutton.
6commandAssociates a function that runs when the Checkbutton state changes.
7cursorChanges the mouse cursor when it moves over the Checkbutton.
8disableforegroundDefines the text color when the Checkbutton is disabled.
9fontSets the font style of the Checkbutton.
10fgSets the foreground or text color of the Checkbutton.
11heightSpecifies the height of the Checkbutton. The default value is 1.
12highlightcolorSets the color of the focus highlight when the Checkbutton is selected.
13imageDisplays an image on the Checkbutton.
14justifyControls text alignment when multiline text is used.
15offvalueSpecifies the value assigned when the Checkbutton is unchecked. The default is 0.
16onvalueSpecifies the value assigned when the Checkbutton is checked. The default is 1.
17padxSets the horizontal padding inside the Checkbutton.
18padySets the vertical padding inside the Checkbutton.
19reliefDefines the border style of the Checkbutton. The default is FLAT.
20selectcolorSets the color of the Checkbutton when it is selected. The default is red.
21selectimageDisplays a different image when the Checkbutton is selected.
22stateDefines the state of the Checkbutton, such as NORMAL, DISABLED, or ACTIVE.
23underlineUnderlines a specific character in the Checkbutton label.
24variableConnects the Checkbutton to a Tkinter variable.
25widthSpecifies the width of the Checkbutton in characters.
26wraplengthWraps the Checkbutton text across multiple lines at a specified width.

Tkinter Checkbutton Methods

Tkinter provides several methods that can be used to control and interact with a Checkbutton programmatically. Some commonly used methods are:

SNMethodDescription
1deselect()Unchecks the Checkbutton.
2flash()Flashes the Checkbutton between its active and normal colors.
3invoke()Calls the function associated with the Checkbutton.
4select()Checks the Checkbutton.
5toggle()Switches the Checkbutton between checked and unchecked states.

Example: Creating Checkbuttons in Tkinter

The following example creates multiple Checkbuttons in a Tkinter window. Users can select their preferred programming languages by checking the available options.

from tkinter import *

# Create main window
top = Tk()
top.geometry("250x200")

title_label = Label(
    top,
    text="Select Your Preferred Languages:",
    font=("Arial", 10, "bold")
)
title_label.pack()

# Define Tkinter variables
checkvar1 = IntVar()
checkvar2 = IntVar()
checkvar3 = IntVar()

# Creating Checkbuttons
chkbtn1 = Checkbutton(
    top,
    text="Python",
    variable=checkvar1,
    onvalue=1,
    offvalue=0,
    height=2,
    width=10
)

chkbtn2 = Checkbutton(
    top,
    text="Java",
    variable=checkvar2,
    onvalue=1,
    offvalue=0,
    height=2,
    width=10
)

chkbtn3 = Checkbutton(
    top,
    text="C++",
    variable=checkvar3,
    onvalue=1,
    offvalue=0,
    height=2,
    width=10
)

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

# Run main event loop
top.mainloop()

Output:

The program creates a simple GUI containing three Checkbuttons labeled Python, Java, and C++. Users can select one or more of these options according to their preferences.

Download New Real Time Projects :- Click here

Conclusion

The Checkbutton is an important Tkinter widget for handling multiple selections in Python GUI applications. With its different configuration options and useful methods, developers can customize its appearance and behavior according to the application’s requirements.

Whether you are building forms, settings pages, or preference-based interfaces, the Checkbutton provides a simple and effective way to collect user input.

For more Python GUI tutorials and advanced Tkinter projects, stay tuned to UpdateGadh!


SEO Keywords

python tkinter checkbutton example
python tkinter checkbutton get value
python tkinter checkbutton tutorial
tkinter checkbutton set value
tkinter checkbutton command
tkinter checkbutton state
tkinter checkbutton select
tkinter checkbutton default value
python tkinter checkbutton
python tkinter checkbutton default checked
python tkinter checkbutton get value
python tkinter checkbutton example
python tkinter checkbutton command
python tkinter checkbutton size
python tkinter checkbutton disable

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us