The Checkbutton is used to track the user's choices provided to the application. In other words, we can say that Checkbutton is used to implement the on/off selections.
The Checkbutton can contain the text or images. The Checkbutton is mostly used to provide many choices to the user among which, the user needs to choose the one. It generally implements many of many selections.
The syntax to use the checkbutton is given below.
A list of possible options is given below.
SN |
Option |
Description |
1 |
activebackground |
It represents the background color when the checkbutton is under the cursor. |
2 |
activeforeground |
It represents the foreground color of the checkbutton when the checkbutton is under the cursor. |
3 |
bg |
The background color of the button. |
4 |
bitmap |
It displays an image (monochrome) on the button. |
5 |
bd |
The size of the border around the corner. |
6 |
command |
It is associated with a function to be called when the state of the checkbutton is changed. |
7 |
cursor |
The mouse pointer will be changed to the cursor name when it is over the checkbutton. |
8 |
disableforeground |
It is the color which is used to represent the text of a disabled checkbutton. |
9 |
font |
It represents the font of the checkbutton. |
10 |
fg |
The foreground color (text color) of the checkbutton. |
11 |
height |
It represents the height of the checkbutton (number of lines). The default height is 1. |
12 |
highlightcolor |
The color of the focus highlight when the checkbutton is under focus. |
13 |
image |
The image used to represent the checkbutton. |
14 |
justify |
This specifies the justification of the text if the text contains multiple lines. |
15 |
offvalue |
The associated control variable is set to 0 by default if the button is unchecked. We can change the state of an unchecked variable to some other one. |
16 |
onvalue |
The associated control variable is set to 1 by default if the button is checked. We can change the state of the checked variable to some other one. |
17 |
padx |
The horizontal padding of the checkbutton |
18 |
pady |
The vertical padding of the checkbutton. |
19 |
relief |
The type of the border of the checkbutton. By default, it is set to FLAT. |
20 |
selectcolor |
The color of the checkbutton when it is set. By default, it is red. |
21 |
selectimage |
The image is shown on the checkbutton when it is set. |
22 |
state |
It represents the state of the checkbutton. By default, it is set to normal. We can change it to DISABLED to make the checkbutton unresponsive. The state of the checkbutton is ACTIVE when it is under focus. |
24 |
underline |
It represents the index of the character in the text which is to be underlined. The indexing starts with zero in the text. |
25 |
variable |
It represents the associated variable that tracks the state of the checkbutton. |
26 |
width |
It represents the width of the checkbutton. It is represented in the number of characters that are represented in the form of texts. |
27 |
wraplength |
If this option is set to an integer number, the text will be broken into the number of pieces. |
The methods that can be called with the Checkbuttons are described in the following table.
from tkinter import *
top = Tk()
top.geometry("200x200")
checkvar1 = IntVar()
checkvar2 = IntVar()
checkvar3 = IntVar()
chkbtn1 = Checkbutton(top, text = "C", variable = checkvar1, onvalue = 1, offvalue = 0, height = 2, width = 10)
chkbtn2 = Checkbutton(top, text = "C++", variable = checkvar2, onvalue = 1, offvalue = 0, height = 2, width = 10)
chkbtn3 = Checkbutton(top, text = "Java", variable = checkvar3, onvalue = 1, offvalue = 0, height = 2, width = 10)
chkbtn1.pack()
chkbtn2.pack()
chkbtn3.pack()
top.mainloop()