Python Tkinter CheckButton


In this tutorial, we will learn about Python Tkinter CheckButton. The Python Tkinter CheckButtons are used to show options to the user to choose. We can add any number of CheckButtons according to the requirement of the application. The important thing to note about checkbox is the user can choose multiple checkboxes from given options. If you want to user choose the only single option you can use the radio button.

Syntax to add Python Tkinter CheckButton:

w = Checkbutton ( master, option, ... )
  • master: This represents the parent window.
  • options: Here is the list of most commonly used options for this widget.

Example Program:

from tkinter import *

mainWindow = Tk()  

#setting size of main window 
mainWindow.geometry("200x200")

cb1 = Checkbutton(mainWindow, text = "Python", \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
cb2 = Checkbutton(mainWindow, text = "Java", \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
cb1.pack()
cb2.pack()
mainWindow.mainloop()

Various possible options in Python Tkinter CheckButton:

Option Description
activebackground This option helps us to set background color of widget when on cursor hover.
activeforeground This option helps us to set foreground color of widget when on cursor hover.
bg This option helps us to set normal background color of label and indicator.
bd This option helps us to set the border size around indicator.
fg This option helps us to set normal foreground colour of widget.
height This option helps us to set height of widget.
command This option helps us to mention a function to every time when checkbutton will change its state.
cursor This option helps us to set the style of cursor like an arrow, dot etc
font This option helps us to set the style of font.
justify This helps us to automatically organize the text in multiple lines.
padx This option helps us to set space left and right between text and checkbox.
pady This option helps us to set space above and below.
relief This helps us to set the style of the border by which is default Flat.
width This helps us to set the width of the widget.

Python Tkinter CheckButton Methods:

After learning about various available options in Python Tkinter CheckButton. Its time to check out some available methods for Python Tkinter CheckButton widget.

METHOD DESCRIPTION
deselect() This method called to deselect the CheckButton.
flash() This method is used to flash CheckButton between normal and active state.
invoke() This method helps us to invoke the associated method with CheckButton.
select() This method is used to select the CheckButton.
toggle() This method helps us to toggle between different CheckButton.


Spread the love:

Please share this page on social media with your friends..

Facebook share Twitter Linkedin share