CHECKBUTTON IN TKINTER
In this tutorial, we will learn about the checkbutton feature in Tkinter and understand its relevancy in terms of providing options and power to user for selection.
Recommended Book: Python GUI Programming with Tkinter
What is Checkbutton in Tkinter?
The checkbutton in tkinter feature is used to toggle between many options as a user. The user has the freedom to choose one or more option by selecting the relevant checkbuttons.
checkbutton_tk = Checkbutton(window, features)
Discover how to create visually stunning and feature-rich applications by using Python’s built-in Tkinter GUI toolkit
Checkbutton features and properties are:
1 activebackground Shows the background color of the check button upon the mouse hover 2 activeforeground Shows the foreground color of the checkbutton upon the mouse hover 3 bg Displays the background color of the checkbutton 4 bitmap It displays an image (monochrome) on the button. 5 bd Show the border size of the checkbutton. 6 command It is used to set the function call whenever it is called 7 cursor You can use the cursor as different shapes on the screen as circle, dot etc. 8 disableforeground It shows the color of the text of the disabled checkbutton. 9 font It shows the font of the checkbutton. 10 fg Determines the foreground color of font that is used for the checkbuttons. 11 height It represents the height of the checkbutton (number of lines). The default height is 1. 12 highlightcolor It shows the highlight color of the checkbutton when it is clicked. 13 image It is used to set image on the checkbutton 14 justify It is used to display text on button in RIGHT, LEFT and CENTER justified positions. 15 offvalue By default, the offvalue of the checkbutton will be 0 unless you set some other value to it. 16 onvalue By default, the offvalue of the checkbutton will be 1 unless you set some other value to it. 17 padx Adds padding in the horizontal direction 18 pady Adds padding in the vertical direction 19 relief This displays different types of borders. By default, it has a FLAT border. 20 selectcolor Sets the color of the checkbutton, by default it is set to red. 21 selectimage Shows the image on the checkbutton upon setting/selecting. 22 state This represents DISABLED or ACTIVE state of the button 23 underline It shows the index of the characters in the text which are to be underlined. The indexing always starts at 0. 24 variable It shows the control variable which is used to monitor the state of the checkbutton 25 width Shows the width of the checkbutton. 26 wraplength Sets the wrap length of the number of characters to be used.
Creating Checkbuttons Widget
import tkinter as tk screen = tk.Tk() #Screen size screen.geometry('300x300') value1 = tk.IntVar() value2 = tk.IntVar() value3 = tk.IntVar() chkbutton_1 = tk.Checkbutton(screen, text = "Apple", variable = value1, onvalue = 1, offvalue = 0, height = 2, width = 10) chkbutton_2 = tk.Checkbutton(screen, text = "Orange", variable = value2, onvalue = 1, offvalue = 0, height = 2, width = 10) chkbutton_3 = tk.Checkbutton(screen, text = "Grape", variable = value3, onvalue = 1, offvalue = 0, height = 2, width = 10) chkbutton_1.pack() chkbutton_2.pack() chkbutton_3.pack() screen.mainloop()Output:
For more references go here