Python Tkinter Checkbutton

python tutorials and learn python

Created with Sketch.

Python Tkinter Checkbutton

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.

Syntax

  1. w = checkbutton(master, options)

A list of possible options is given below.

SNOptionDescription
1activebackgroundIt represents the background color when the checkbutton is under the cursor.
2activeforegroundIt represents the foreground color of the checkbutton when the checkbutton is under the cursor.
3bgThe background color of the button.
4bitmapIt displays an image (monochrome) on the button.
5bdThe size of the border around the corner.
6commandIt is associated with a function to be called when the state of the checkbutton is changed.
7cursorThe mouse pointer will be changed to the cursor name when it is over the checkbutton.
8disableforegroundIt is the color which is used to represent the text of a disabled checkbutton.
9fontIt represents the font of the checkbutton.
10fgThe foreground color (text color) of the checkbutton.
11heightIt represents the height of the checkbutton (number of lines). The default height is 1.
12highlightcolorThe color of the focus highlight when the checkbutton is under focus.
13imageThe image used to represent the checkbutton.
14justifyThis specifies the justification of the text if the text contains multiple lines.
15offvalueThe 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.
16onvalueThe 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.
17padxThe horizontal padding of the checkbutton
18padyThe vertical padding of the checkbutton.
19reliefThe type of the border of the checkbutton. By default, it is set to FLAT.
20selectcolorThe color of the checkbutton when it is set. By default, it is red.
21selectimageThe image is shown on the checkbutton when it is set.
22stateIt 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.
24underlineIt represents the index of the character in the text which is to be underlined. The indexing starts with zero in the text.
25variableIt represents the associated variable that tracks the state of the checkbutton.
26widthIt represents the width of the checkbutton. It is represented in the number of characters that are represented in the form of texts.
27wraplengthIf this option is set to an integer number, the text will be broken into the number of pieces.

Methods

The methods that can be called with the Checkbuttons are described in the following table.

SNMethodDescription
1deselect()It is called to turn off the checkbutton.
2flash()The checkbutton is flashed between the active and normal colors.
3invoke()This will invoke the method associated with the checkbutton.
4select()It is called to turn on the checkbutton.
5toggle()It is used to toggle between the different Checkbuttons.

Example

  1. from tkinter import *
  2. top = Tk()
  3. top.geometry(“200×200”)
  4. checkvar1 = IntVar()
  5. checkvar2 = IntVar()
  6. checkvar3 = IntVar()
  7. chkbtn1 = Checkbutton(top, text = “C”, variable = checkvar1, onvalue = 1, offvalue = 0, height = 2, width = 10)
  8. chkbtn2 = Checkbutton(top, text = “C++”, variable = checkvar2, onvalue = 1, offvalue = 0, height = 2, width = 10)
  9. chkbtn3 = Checkbutton(top, text = “Java”, variable = checkvar3, onvalue = 1, offvalue = 0, height = 2, width = 10)
  10. chkbtn1.pack()
  11. chkbtn2.pack()
  12. chkbtn3.pack()
  13. top.mainloop()

Output:

Python Tkinter Checkbutton

 

Leave a Reply

Your email address will not be published. Required fields are marked *