python Checkbotton

The Checkbutton widget in Tkinter allows users to make on/off or true/false selections. It is widely used when multiple options are provided, and the user is allowed to choose one or more options. The state of a checkbutton can either be checked or unchecked, and this can be tracked using a control variable.

Syntax:

w = Checkbutton(master, options)

Here, master refers to the parent widget (such as a window or frame), and options are used to customize the Checkbutton.

Options for Tkinter Checkbutton:

SN Option Description
1 activebackground Background color when the checkbutton is hovered over with the cursor.
2 activeforeground Foreground color when the checkbutton is hovered over with the cursor.
3 bg Background color of the checkbutton.
4 bitmap Displays a monochrome image on the checkbutton.
5 bd Specifies the size of the border around the checkbutton.
6 command Function to be called when the state of the checkbutton is changed.
7 cursor Changes the cursor appearance when hovered over the checkbutton.
8 disableforeground Foreground color of the text when the checkbutton is disabled.
9 font Specifies the font of the checkbutton text.
10 fg Foreground color (text color) of the checkbutton.
11 height Specifies the height of the checkbutton (number of lines). Default is 1.
12 highlightcolor The color of the focus highlight when the checkbutton is under focus.
13 image Image used to represent the checkbutton.
14 justify Specifies the justification of text if it contains multiple lines.
15 offvalue The value assigned to the variable when the checkbutton is unchecked (default is 0).
16 onvalue The value assigned to the variable when the checkbutton is checked (default is 1).
17 padx Horizontal padding of the checkbutton.
18 pady Vertical padding of the checkbutton.
19 relief Type of the checkbutton border (FLAT, SUNKEN, RAISED, GROOVE, RIDGE). Default is FLAT.
20 selectcolor Specifies the color of the checkbutton when it is checked. Default is red.
21 selectimage Image to be shown when the checkbutton is checked.
22 state State of the checkbutton (NORMAL, DISABLED, ACTIVE).
23 underline Index of the character in the text to be underlined (indexing starts from 0).
24 variable Control variable (e.g., IntVar) that tracks the state of the checkbutton (whether it’s checked or not).
25 width Width of the checkbutton, represented in the number of text characters.
26 wraplength If set, the text will wrap to fit within the given width in pixels.

Methods Associated with Checkbuttons:

SN Method Description
1 deselect() Turns off the checkbutton (unchecked state).
2 flash() Flashes the checkbutton between the active and normal colors.
3 invoke() Invokes the function associated with the command option of the checkbutton.
4 select() Turns on the checkbutton (checked state).
5 toggle() Toggles the state of the checkbutton between checked and unchecked.

Example of Using a Tkinter Checkbutton:

from tkinter import *

# Function to show the state of the checkbutton
def show_selection():
    if var.get() == 1:
        label.config(text="Checkbutton is Checked")
    else:
        label.config(text="Checkbutton is Unchecked")

# Create the main window
root = Tk()

# Control variable for checkbutton
var = IntVar()

# Create a checkbutton widget
check_button = Checkbutton(root, text="Check Me", variable=var, onvalue=1, offvalue=0, command=show_selection)

# Create a label to display the state
label = Label(root, text="Checkbutton is Unchecked")

# Display widgets
check_button.pack()
label.pack()

# Run the application
root.mainloop()

Explanation of the Example:

Checkbutton: A checkbutton is created with the text "Check Me". The state of the checkbutton is tracked by the control variable var, which holds the value 1 when checked and 0 when unchecked.

Command: The show_selection() function is called whenever the checkbutton state changes. It updates the label text to display whether the checkbutton is checked or unchecked.<

This example demonstrates how to use the Checkbutton widget in a Tkinter GUI, along with handling its state using a control variable.