Home ยป Python Tkinter Check Button

Python Tkinter Check Button

The Checkbutton widget is used to display a number of options to the user as toggle buttons.

The user can then select one or more options by clicking the button corresponding to each option.

The Checkbutton also allows you to display images instead of text.

Syntax

w = checkbutton(master, options)

A list of possible options is given below.

Option Description
activebackground This represents the background color when the checkbutton is under the cursor.
activeforeground This represents the foreground color of the checkbutton when the checkbutton is under the cursor.
bg The background color of the button.
bitmap This displays an image on the button.
bd The size of the border around the corner.
command This is associated with a function to be called when the state of the checkbutton is changed.
cursor The mouse pointer will be changed to the cursor name when it is over the checkbutton.
disableforeground This is the color which is used to represent the text of a disabled checkbutton.
font This represents the font of the checkbutton.
fg This foreground color of the checkbutton.
height This represents the height of the checkbutton. The default height is 1.
highlightcolor The color of the focus highlight when the checkbutton is under focus.
image The image used to represent the checkbutton.
justify This specifies the justification of the text if the text contains multiple lines.
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.
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.
padx The horizontal padding of the checkbutton
pady The vertical padding of the checkbutton.
relief The type of border of the checkbutton. The default is FLAT.
selectcolor The color of the checkbutton when it is set. The default is red.
selectimage The image which is shown on the checkbutton when it is set.
state This represents the state of the checkbutton. By default, it is normal. We can change it to DISABLED to make the checkbutton unresponsive. The state of the checkbutton is ACTIVE when it is in focus.
underline This represents the index of the character in the text which is to be underlined. The indexing starts at zero in the text.
variable This represents the associated variable that tracks the state of the checkbutton.
width This represents the width of the checkbutton. It is represented in the number of characters that are represented in the form of texts.
wraplength If 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 a Checkbutton

Method Description
deselect() This is called to turn off the checkbutton.
flash() This checkbutton is flashed between active and normal colors.
invoke() This will invoke the method associated with the checkbutton.
select() This is called to turn on the checkbutton.
toggle() This is used to toggle between the different Checkbuttons.

 

Code examples

 

from tkinter import *

top = Tk()
top.title('maxpython')
top.geometry('200x250')

def isChecked():
    return Label(top, text=f'Checkbutton is checked: , {checkvar1.get()}').pack()
    

checkvar1 = BooleanVar()
chkbtn = Checkbutton(top, text='Check Me',variable = checkvar1, onvalue=True, offvalue=False, command=isChecked)
chkbtn.pack()


top.mainloop()
from tkinter import * 
  
root = Tk()
root.geometry("300x200")
  
w = Label(root, text ='GeeksForGeeks', font = "50") 
w.pack()
  
Checkbutton1 = IntVar()  
Checkbutton2 = IntVar()  
Checkbutton3 = IntVar()
  
Button1 = Checkbutton(root, text = "English", 
                      variable = Checkbutton1,
                      onvalue = 1,
                      offvalue = 0,
                      height = 2,
                      width = 10)
  
Button2 = Checkbutton(root, text = "French",
                      variable = Checkbutton2,
                      onvalue = 1,
                      offvalue = 0,
                      height = 2,
                      width = 10)
  
Button3 = Checkbutton(root, text = "German",
                      variable = Checkbutton3,
                      onvalue = 1,
                      offvalue = 0,
                      height = 2,
                      width = 10)  
    
Button1.pack()  
Button2.pack()  
Button3.pack()
  
mainloop() 

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More