Last Updated : 11 Jul, 2025
Tkinteris a GUI (Graphical User interface) module which is used to create various types of applications. It comes along with the Python and consists of various types of widgets which can be used to make GUI more attractive and user-friendly.
Checkbuttonis one of the widgets which is used to select multiple options.
Checkbuttoncan be created as follows:
chkbtn = ttk.Checkbutton(parent, value = options, ...)Code #1: Python3 1==
# This will import tkinter and ttk
from tkinter import * from tkinter import ttk
root = Tk()
# This will set the geometry to 200x100
root.geometry('200x100')
text1 = StringVar()
text2 = StringVar()
# These text are used to set initial
# values of Checkbutton to off
text1.set('OFF')
text2.set('OFF')
chkbtn1 = ttk.Checkbutton(root, textvariable = text1, variable = text1,
offvalue = 'GFG Not Selected',
onvalue = 'GFG Selected')
chkbtn1.pack(side = TOP, pady = 10)
chkbtn2 = ttk.Checkbutton(root, textvariable = text2, variable = text2,
offvalue = 'GFG Average',
onvalue = 'GFG Good')
chkbtn2.pack(side = TOP, pady = 10)
root.mainloop()
Output #1:
When you run application you see the initial states of
Checkbuttonas shown in output.
Output #2:As soon as you
selectthe
Checkbuttonyou'll see that text has been changed as in output.
Output #3:When you
deselectthe
Checkbuttonyou'll again observe following changes.
Code #2:Commands can be integrate with the
Checkbuttonwhich can be execute when checkbutton is selected or deselected depending upon conditions.
Python3 1==
# Importing tkinter, ttk and
# _show method to display
# pop-up message window
from tkinter import * from tkinter import ttk
from tkinter.messagebox import _show
root = Tk()
root.geometry('200x100')
text1 = StringVar()
text1.set('OFF')
# This function is used to display
# the pop-up message
def show(event):
string = event.get()
_show('Message', 'You selected ' + string)
chkbtn1 = ttk.Checkbutton(root, textvariable = text1, variable = text1,
offvalue = 'GFG Good',
onvalue = 'GFG Great',
command = lambda : show(text1))
chkbtn1.pack(side = TOP, pady = 10)
root.mainloop()
Output: Note:
In above code
offvalueand
onvalueare used to set the values of
Checkbuttonof non-selected state and selected state respectively.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4