Python Tkinter - check button

Pybeginner
By -
0



We can also add a check button into tkinter. To create a check button widget, you can use the Checkbutton class like this:

chek = Checkbutton(window, text = 'choose')

 

In addition, you can set the checked status by passing the check value to the Check button like this:

 

from tkinter import *
from tkinter.ttk import *

window = Tk ()
window .title ("Check button")
window.geometry('250x250')

state = BooleanVar ()
state.set (True) #set check state

chek = Checkbutton (window, text ='choice', var = state)
chek.grid (column = 0, row = 0,padx=15, pady=15)

window.mainloop ()




 

Python Tkinter - Check button events

 

We can use 3 types of variables when passing an event to the check button which are 

IntVar ()
StringVar ()
BooleanVar ()

 

Python Tkinter - Event capture checkbox click using BooleanVar ()

 

Here BooleanVar (), accepts only True or False (True or False), (0 or 1), otherwise, it will present an error, and every time you check and uncheck, a message will be printed as False or True.

Therefore, the complete code will look like this:

 

from tkinter import *
from tkinter.ttk import *
window = Tk ()
window.title("First Checkbutton")
window.geometry ('200x200')

def event_1 ():
result = state_1.get ()
label.configure(text=result)
print('Checkbox value:', result )


label = Label(window, text="text")
label.grid(row=0,column=0, padx=10, pady=10)

state_1 = BooleanVar ()
state_1.set (True) # set check state
chek_1 = Checkbutton (window, text = 'State_1', var = state_1, onvalue = 1, offvalue = 0, command = event_1)
chek_1.grid (column = 0, row = 1, padx=10, pady=10)

window.mainloop ()

 



Python Tkinter - Event capture click of checkbox using StringVar ()

 

Here we will do the same procedure, only instead we will use StringVar ().

Attention: StringVar() accepts any value, but these values will be considered as strings.

Therefore, the complete code will look like this:

 

from tkinter import *
from tkinter.ttk import *
window = Tk ()
window.title("First Checkbutton")
window.geometry ('200x200')

def event_2 ():
result = state_2.get()
label.configure(text=result)
print('Checkbox value:', result )


label = Label(window, text="text")
label.grid(row=0,column=0, padx=10, pady=10)

state_2 = StringVar()
state_2.set(True) # set check state
chek_2 = Checkbutton(window, text =' State_2 ', var = state_2, onvalue =' Programming Road ', offvalue =' Not Programming Road ', command = event_2)
chek_2.grid(column = 0, row = 1,padx=10, pady=15)

window.mainloop ()

 



 

Python Tkinter - Capture checkbox click event using IntVar ()

 

Here we will do the same procedure, only instead, we will use IntVar ().

Attention: IntVar() accepts only integer values, otherwise it will present an error.

Therefore, the complete code will look like this:

 

from tkinter import *
from tkinter.ttk import *
window = Tk()
window.title("First Checkbutton")
window.geometry('200x200')

def event_3():
result = state_3.get()
label.configure(text=result)
print('Checkbox value:', result )


label = Label(window, text="text")
label.grid(row=0,column=0, padx=10, pady=10)

state_3 = IntVar()
chek_3 = Checkbutton(window, text =' State_3 ', var = state_3, onvalue = 98, offvalue = 0, command = event_3)
chek_3.grid(column = 0, row =1,padx=10, pady=15)

window.mainloop()

 



Python tkinter - How to set the check state of a check button

 

You can set the initial state of a check box to make it checked or unchecked.

 

chek_state = IntVar ()
chek_state.set (0) # uncheck
chek_state.set (1) # check

Post a Comment

0Comments

Post a Comment (0)