Hit enter after type your search item

Python tkinter Checkbutton

The Checkbutton in tkinter

The Checkbutton widget allows a user selecting one or more given options from the group of options.

For example, which programming languiages do you like?

  • C#
  • Python
  • Java
  • PHP
  • Kotlin
  • Go Lang

So, a user can select one or more languages from the toggle buttons.

In order to allow users to choose only one option from two or more, you may use the tkinter Radiobutton widget.

How Checkbox button is created in tkinter

You may create Checkbutton by using this syntax:

w = Checkbutton ( master, option, … )

Go through the section below for examples of creating Checkbuttons with various available options in tkinter – a Python library.

A simple Checkbutton example

In the Python program below, we have created five Checkbuttons. For each check button, we set the text, onvalue, and offvalue options. Have a look at this simple example:




An example of foreground and background colors of the Check button

By using bg option of the Checkbutton widget, you may set the background color – behind the label and indicator.

Similarly, to set the text color of the Checkbutton, use the fg option.

See an example below:





Setting active foreground and active background of check buttons

In this program, we set the active foreground and background colors of the check buttons. That means if you click/press the check button, the color of the checkbox’s text and background changes.

Click on any of the check buttons to see its impact:




Using the relief option example

In order to stand out checkbutton differently, you may set the style by using the relief option.

The following are the possible values for the relief option:

  • flat (default)
  • groove
  • raised
  • ridge
  • solid
  • sunken

The example below uses a few of these values – you can its output in the image below:




How to get value of the checkbox

In real applications, you will need to get the values of the checkbox in order to process the information or do something.

For that, you need to use onvalue and offvalue options of the check button.

Besides, variables are declared and assigned as creating the checkbuttons.

Have a look at the program below where we have the same set of options as in the above examples.

After selecting the check buttons, press the tkinter button “Display Values” and it will print in the console the value for each check button.






How did it work?

  • A function is defined to display the value of each checkbutton, called display_check().
  • Then we declared five empty int type variables
  • As we created five check buttons, we set onvalue= 1 and offvalue=0 for each check button.
  • We also used the variable option where we assigned the declared variable to each checkbutton.
  • Finally, a button is created that will call display_check custom function – it prints the selected value of the check button in the console.

Note: Rather than 1/0 for the on and off values, you may set other values for the check buttons.

This div height required for enabling the sticky sidebar