Python What is TKinter Checkbox how to Create Checkbox
Published December 11, 2021Checkbox widgets are extensively used in online applications when the user is required to make multiple choices. They are identical to on/off switches in that they enable users to make a variety of choices from the provided list. If you want to display selectable checkboxes in your Python program, you can think of the Tkinter checkbox widgets.
This article will explore more about the Tkinter checkbox and how it is used in a Python program.
What is the Tkinter checkbox?
The Tkinter Checkbutton widgets are used to present a python application user with a variety of alternatives as toggle buttons. The user may then choose one or even more alternatives by simply clicking the button next to each one.
You may also use the Tkinter checkbox to display photos, not just words.
The Syntax of Tkinter checkbox
To create this widget, you only need to follow the following simple syntax:
w = Check button (root, checkbox_options )
Where root refers to the parent window and options, refer to the widget's settings. This widget may be used with a variety of options. Here are some common options to consider for your Python application:
-
Activebackground - exhibits the appearance whenever the cursor hovers over the check button box.
-
Activeforeground - shows the foreground color while the cursor rests above the check button.
-
Bg- the option provides the regular background color for use behind indication as well as the label.
-
Bitmap - shows the monochromatic images on the checkbox
-
Bd- specifies the border of the checkbox
-
Command- these are procedures called every time the user clicks on the checkbox.
-
Font - specifies the font to be used on the checkbox
-
Fg- specifies the color to render the text
-
Height- specifies the lines to be used on the check button
-
Justify- controls how the text is justified on the page either at the RIGHT, LEFT, or CENTER
-
Text- contains text to be displayed on the checkbox
-
Width- determines the size of the text or image displayed on the checkbox.
-
State – specifies the state of the check button. It can be state=NORMAL, state=ACTIVE or state=ACTIVE.
The Tkinter checkbox Methods
The following methods are used to create the Tkinter checkbox widgets:
-
Flash()- flashes on and off the active and normal colors of the checkbox button
-
Deselect()- turns off the button
-
Invoke()- calls for the on click action on the checkbox
-
Select()- turns on the check button
-
Toggle()- clears the check button on the condition all sets are cleared
Tkinter Checkbox Example
The following code sample shows the Tkinter checkbox, which enables users to choose between C++ and Python as their preferred programming language
import tkinter as tk window = tk.Tk() window.title('select your Favourite Language') window.geometry('600x600') l = tk.Label(window, bg='white', width=20, text='empty') l.pack() def print_selection(): if (var1.get() == 1) & (var2.get() == 0): l.config(text='I love Python ') elif (var1.get() == 0) & (var2.get() == 1): l.config(text='I love C++') elif (var1.get() == 0) & (var2.get() == 0): l.config(text='I do not anything') else: l.config(text='I love both') var1 = tk.IntVar() var2 = tk.IntVar() c1 = tk.Checkbutton(window, text='Python', variable=var1, onvalue=1, offvalue=0, command=print_selection) c1.pack() c2 = tk.Checkbutton(window, text='C++', variable=var2, onvalue=1, offvalue=0, command=print_selection) c2.pack() window.mainloop() |
If you execute the above code in your Python IDE, the following checkbox will pop up on your
![]() |
Article Contributed By :
|
|
|
|
1145 Views |