python
  1. python-checkbutton

Python Checkbutton

A Checkbutton is a widget in Python that allows the selection or deselection of an option. It appears as a square box with text beside it.

Syntax

tk.Checkbutton(parent, option=value, ...)

Example

import tkinter as tk

root = tk.Tk()

# creating Checkbutton
c1 = tk.Checkbutton(root, text="Option 1")
c1.pack()

root.mainloop()

Output

output-checkbutton

Explanation

In the above example, we import the tkinter library and create a root window using the tk.Tk() method. Then, we create a Checkbutton widget using the tk.Checkbutton() method and pass it the parent window (root) and some options. Here, we only pass the text option to provide the label for the Checkbutton. Lastly, we use the pack() method to display the Checkbutton on the window.

Use

Checkbuttons can be used to allow user to select any number of options from the given set of options.

Important Points

  • The text option is used to display the label for the Checkbutton.
  • The variable option can be set to store the Checkbutton value (True or False) in a variable.
  • The onvalue and offvalue option can be used to specify the values to be stored when the Checkbutton is selected or deselected respectively.
  • The command option can be used to specify the callback function that is called when the Checkbutton is selected or deselected.

Summary

In summary, a Checkbutton is a Python widget that is used to provide a set of options for selection or deselection. It can be customized using various options such as text, variable, onvalue, offvalue, and command.

Published on: