Checkbutton Tkinter

Tkinter module in python has rich library of GUI widget, Checkbutton is one of them. Checkbutton is also known as checkbox, it gives us option to select multiple values at a time. We need to assign different values for different check box variables using IntVar() function. Below example will show you everything about checkbox in tkinter.

from tkinter import *
win=Tk()
win.geometry('200x200')
ch1=IntVar()
Checkbutton(win, text="male", variable=ch1).grid(row=0,column=0)
ch2=IntVar()
Checkbutton(win, text="female",variable=ch2).grid(row=0,column=1)
mainloop()

Get Checkbutton Values (selected checkbox)

from tkinter import *
def func():
	val=""
	if ch1.get()==1:
		val+="Java "
	if ch2.get()==1:
		val+="Python"
	lbl.configure(text=val)


win=Tk()
win.title("CoderMantra Tutorial")
win.geometry('300x200')
ch1=IntVar()
ch2=IntVar()
l1=Label(win,text="Your Favourite Programming Language")
l1.grid(row=0,column=0)
r1=Checkbutton(win,text="Java",variable=ch1)
r1.grid(row=1,column=0)
r2=Checkbutton(win,text="Python",variable=ch2)
r2.grid(row=2,column=0)
b=Button(win,text="Click Here",command=func)
b.grid(row=3,column=0)
lbl=Label(win,text="")
lbl.grid(row=4,column=0)
win.mainloop()

Mini Quiz

0%

Tkinter - Quiz 6

1 / 5

Q1. What is the main purpose of the Checkbutton widget in Tkinter?

2 / 5

Q2. Which control variable is commonly used with a Checkbutton in Tkinter?

3 / 5

Q3. Which method is used to check whether a Checkbutton is selected?

4 / 5

Q4. What value does IntVar().get() return when a Checkbutton is selected?

5 / 5

Q5. Which widget is more suitable when a user needs to select multiple options?

Your score is

The average score is 0%

0%

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top