Python Tkinter Checkbutton Widget
In this post, you will learn about the Python Tkinter checkbutton widget.
Tkinter is a standard cross-platform package for creating GUIs. It is also called Tk interface. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. Tkinter is rich in widgets, like radiobutton, checkbutton, menu, frame, canvas and much more.
In this article, you will learn about the checkbutton widget. It allows user to select one or more items. Checkbuttons should normally have a variable (IntVar) assigned to the variable option. It allows you to determine the state of the checkbutton either checked or not. In addition, we can bind a callback function to the button which will be called when the button is pressed.
Syntax of Tkinter Checkbutton
w = Checkbutton ( master, options)
These are the possible options-
Options | Description |
activebackground | To represent the background color when the checkbutton is under the cursor. |
activeforeground | To represent the foreground color when the checkbutton is under the cursor. |
bg | To represent the normal background color. |
bitmap | To display a monochrome image on a button. |
bd | To represent the size of the border around the indicator. |
command | A function or method that is called when the button is pressed. |
cursor | To show a cursor when the mouse pointer is moved over the button. |
disabledforeground | To use the color when the button is disabled. |
font | To represent the font used for the text. |
fg | To represent the color used to render the text. |
height | The number of lines of text on the checkbutton. |
highlightcolor | To represent the color of the focus highlight when the checkbutton has the focus. |
image | To display an image on the widget. |
justify | To align multiple lines of text, i.e., LEFT, RIGHT, CENTER. |
offvalue | The value corresponding to a non-checked button. Default is 0. |
onvalue | The value corresponding to a checked button. Default is 1. |
padx | The leave space to the left and right of the checkbutton and text. |
pady | The leave space to the top and button of the checkbutton and text. |
selectcolor | The color of the checkbutton when it is set. Default is red. |
selectimage | To set the image to appear in the checkbutton. |
state | It represents the state of the checkbutton. |
text | The text to display in the button. |
underline | To represent the index of the character in the text which is to be underlined. |
variable | To represent the associated variable that tracks the state of the checkbutton. |
width | To represent the width of the checkbutton. |
Checkbutton Methods
These are some commonly used methods for checkbutton widget.
METHODS | Description |
deselect() | To deselects the checkbox, i.e., sets the value to offvalue. |
flash() | To flashes the checkbox between the active and normal colors. |
invoke() | To invokes the method associated with the checkbutton. |
select() | To turn on the checkbutton. |
toggle() | To toggle between the different checkbuttons. |
Example of Tkinter Checkbutton Widget
The given program adds three checkbuttons to the window.
from tkinter import *
def getSelected():
print("Mango is", x1.get())
print("Orange is", x2.get())
print("Pineapple is", x3.get())
window = Tk()
window.title('Tkinter Checkbutton')
window.geometry('300x150')
f = Frame(relief=RAISED , borderwidth=5)
x1 = IntVar()
x2 = IntVar()
x3 = IntVar()
c1 = Checkbutton(
f, text="Mango",
variable=x1,
command= (lambda: getSelected()))
c1.pack(side=LEFT)
c2 = Checkbutton(
f, text="Orange",
variable=x2,
command= (lambda: getSelected()))
c2.pack(side=LEFT)
c3 = Checkbutton(
f, text="Pineapple",
variable=x3,
command= (lambda: getSelected()))
c3.pack(side=LEFT)
f.pack()
mainloop()
Related Articles
Python Tkinter Tutorial with ExamplesPython Tkinter Combobox Event Binding
Python Tkinter Geometry Managers
Python3 Tkinter Messagebox
Python Tkinter Frame Widget
Python Tkinter Scale Widget
Entry Field Validation in Tkinter Python
Python Tkinter Text Widget
Python Tkinter Checkbutton Widget
Python 3 Tkinter Menu Bar
Add background image in Python Tkinter
OpenCV histogram equalization color
Color histogram Python OpenCV
Histogram of grayscale image python
High pass filter OpenCV python
Python OpenCV ColorMap
OpenCV Gaussian blur Python
Python OpenCV Overview and Examples