Python Tkinter Checkbutton – A Complete Guide
Python Tkinter Checkbutton
In GUI applications, user input plays a crucial role, and Tkinter, the standard GUI toolkit in Python, offers various widgets to handle such interactions. One such essential widget is the Checkbutton. The Checkbutton allows users to make multiple selections from a given set of choices. It provides an intuitive on/off switch, making it useful for forms, settings, and preference-based applications.
Complete Python Course with Advance topics:-Click here
What is a Tkinter Checkbutton?
A Checkbutton is a type of button that allows users to select multiple options from a list. It acts as a toggle switch where users can check or uncheck a particular choice. The Checkbutton can display both text and images, making it versatile for different UI needs.
Syntax of Checkbutton
w = Checkbutton(master, options)
Here, master
is the parent widget (like a Tkinter window or frame), and options
are various attributes to customize the Checkbutton.
Tkinter Checkbutton Options
The table below describes the various configuration options available for the Checkbutton widget:
SN | Option | Description |
---|---|---|
1 | activebackground |
Background color when the Checkbutton is hovered. |
2 | activeforeground |
Foreground (text) color when hovered. |
3 | bg |
Background color of the Checkbutton. |
4 | bitmap |
Displays a monochrome image on the Checkbutton. |
5 | bd |
Sets the border size of the Checkbutton. |
6 | command |
Associates a function to be executed when the Checkbutton state changes. |
7 | cursor |
Changes the mouse cursor when hovering over the Checkbutton. |
8 | disableforeground |
Color of the text when the Checkbutton is disabled. |
9 | font |
Sets the font style of the Checkbutton. |
10 | fg |
Foreground (text) color of the Checkbutton. |
11 | height |
Specifies the height of the Checkbutton. Default is 1. |
12 | highlightcolor |
Color of the focus highlight when the Checkbutton is selected. |
13 | image |
Displays an image on the Checkbutton. |
14 | justify |
Aligns text when multiline text is used. |
15 | offvalue |
Value assigned when the Checkbutton is unchecked (default: 0). |
16 | onvalue |
Value assigned when the Checkbutton is checked (default: 1). |
17 | padx |
Horizontal padding inside the Checkbutton. |
18 | pady |
Vertical padding inside the Checkbutton. |
19 | relief |
Border style of the Checkbutton (default: FLAT). |
20 | selectcolor |
Color of the Checkbutton when selected (default: red). |
21 | selectimage |
Displays a different image when the Checkbutton is selected. |
22 | state |
State of the Checkbutton (NORMAL , DISABLED , ACTIVE ). |
23 | underline |
Underlines a specific character in the Checkbutton label. |
24 | variable |
Links the Checkbutton to a Tkinter variable. |
25 | width |
Specifies the width of the Checkbutton in characters. |
26 | wraplength |
Wraps text into multiple lines at a specified width. |
Tkinter Checkbutton Methods
Below are the common methods available for the Checkbutton widget:
SN | Method | Description |
---|---|---|
1 | deselect() |
Unchecks the Checkbutton. |
2 | flash() |
Flashes between active and normal colors. |
3 | invoke() |
Calls the function associated with the Checkbutton. |
4 | select() |
Checks the Checkbutton. |
5 | toggle() |
Toggles the Checkbutton state (checked/unchecked). |
Example: Creating Checkbuttons in Tkinter
Here’s a simple example demonstrating how to create multiple Checkbuttons in a Tkinter window:
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:
This will create a simple GUI with three checkboxes labeled Python, Java, and C++, allowing users to select multiple options.
Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE
Conclusion
The Checkbutton widget in Tkinter is a powerful component for handling multiple-choice selections in Python GUI applications. It supports various configurations and methods to customize user interactions effectively. Whether used in forms, settings, or user preferences, the Checkbutton provides a seamless experience for capturing user input.
For more Python GUI tutorials and advanced Tkinter projects, stay tuned to UpdateGadh!
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
Post Comment