How to Add Checkbox with tkinter in Python


In this example, we will create four check boxes in the window, and align them to the left, and then select the first check box.
The Checkbutton control is used to create checkboxes. The following are the properties of the Checkbutton in tkinter:

  • onvalue and offvalue: Set the variable specified by the variable property of the Checkbutton control, the value is to be stored. If the checkbox is not selected, the value of this variable is offvalue. If the checkbox is checked, the value of this variable is onvalue.
  • indicatoron: Set this property to 0, you can turn the entire control into a checkbox.

The following are the methods of the Checkbutton control:

  • select(): Select the checkbox and set the value of the variable to onvalue.
  • flash(): Interchanges the foreground and background colors to produce a flickering effect.
  • invoke(): Execute the function defined by the command property.
  • toggle(): Change the status of the check button. If the check button is now on, it will be off.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# addCheckbox.py

from tkinter import *

win = Tk()
check1 = Checkbutton(win, text="Google")
check2 = Checkbutton(win, text="Amazon")
check3 = Checkbutton(win, text="Apple")
check4 = Checkbutton(win, text="Facebook")
check1.select()
check1.pack(side=LEFT)
check2.pack(side=LEFT)
check3.pack(side=LEFT)
check4.pack(side=LEFT)
win.mainloop()

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement