tkinter
  1. tkinter-checkbutton

Checkbutton

The Checkbutton is a tkinter input widget that allows the user to select a value from multiple options. It displays a box that the user can check or uncheck to select or deselect the option.

Syntax

check_button = Checkbutton(parent, text="Option", variable=var)

Here, parent refers to the parent window or frame where the check button will be placed. text is the text label that will appear next to the check box. variable is the IntVar(), DoubleVar(), or BooleanVar() variable that stores the value of the check box.

Example

from tkinter import *
    
root = Tk()    
root.geometry("200x200")

var1 = IntVar()
var2 = IntVar()

check_button1 = Checkbutton(root, text="Option 1", variable=var1)
check_button1.pack(pady=5)

check_button2 = Checkbutton(root, text="Option 2", variable=var2)
check_button2.pack(pady=5)

root.mainloop()

In this example, we create a simple window with two check buttons labeled "Option 1" and "Option 2". The state of the check box is stored in var1 and var2 respectively.

Output

When run, the example code above produces a window with two check buttons labeled "Option 1" and "Option 2". The user can select one or both options by checking the respective box.

Explanation

The Checkbutton widget creates a check button with a label next to it. When the user clicks the button, the corresponding variable is set to 1 (if the button is checked) or 0 (if the button is unchecked). The variable option allows the value of the check box to be stored in a variable of the user's choosing.

Use

The Checkbutton widget is useful for presenting a list of options to the user and allowing them to select one or multiple options. It can be used in forms, surveys, or any application that requires user input.

Important Points

  • The variable option for Checkbutton should be set to an instance of IntVar(), DoubleVar(), or BooleanVar() depending on the type of value you want to store.
  • By default, the check button will display the label to the right of the box. To change the position of the label, use the anchor option.

Summary

The Checkbutton widget in tkinter allows the user to select one or multiple options from a list. It stores the value of the check box in a variable and can be used in numerous applications where user input is required.

Published on: