Tkinter Checkbutton is a graphical user interface (GUI) widget that allows the user to select one or more options from a set of choices.
It is a part of the Tkinter library, which is a standard Python library for creating GUIs.
The Checkbutton widget creates a square box and a label next to it. When the user clicks on the box, a checkmark appears in the box to indicate that the option is selected. Clicking the box again removes the checkmark and deselects the option.
To use the Checkbutton widget in a Python program, you first need to:
1- import the Tkinter module:
import tkinter as tk
2-Then you can create a Checkbutton widget by calling the Checkbutton() method and passing it the parent window as the first argument and any additional options as keyword arguments.
Here is an example:
import tkinter as tk root = tk.Tk() # create a Checkbutton widget checkbutton = tk.Checkbutton(root, text="Option 1") # add the Checkbutton widget to the window checkbutton.pack() # start the event loop root.mainloop()
In this example, we create a Checkbutton widget with the label “Option 1” and add it to the root window using the pack() method. When you run the program, you should see a window with a Checkbutton widget labeled “Option 1”.
Here is the syntax to create a Checkbutton widget in Python Tkinter:
Checkbutton(parent, options...)
where:
parent is the parent widget, which is typically the main window or a frame within the main window.
options are the configuration options for the Checkbutton widget, such as text, command, variable, etc.
Here is an example that creates a Checkbutton widget with a label and a variable:
import tkinter as tk root = tk.Tk() # create a variable to store the Checkbutton state check_var = tk.IntVar() # create the Checkbutton widget with label and variable checkbutton = tk.Checkbutton(root, text="Option 1", variable=check_var) # add the Checkbutton widget to the window checkbutton.pack() # start the event loop root.mainloop()
In this example, we create a variable check_var to store the state of the Checkbutton widget. We then create the Checkbutton widget with a label “Option 1” and the variable check_var. This means that when the Checkbutton is selected, the variable check_var will be set to 1, and when it is deselected, the variable will be set to 0. We then add the Checkbutton widget to the root window using the pack() method and start the event loop.
Python Tkinter Checkbutton is an important GUI widget for creating graphical user interfaces in Python.
Here are some of the reasons why:
User input: Checkbutton allows the user to select one or more options from a set of choices.
It is an easy and intuitive way for users to provide input to a program.
UI design: Checkbutton is a simple and effective way to display and organize options in a graphical user interface. It is often used in conjunction with other GUI widgets such as labels, buttons, and entry fields.
Configurable options: Checkbutton provides a wide range of configuration options, such as the text label, the variable associated with the Checkbutton, the command to execute when the Checkbutton is clicked, and more. This allows developers to customize the Checkbutton to suit their specific needs.
Cross-platform compatibility: Python Tkinter Checkbutton is a part of the Tkinter library, which is a standard Python library that is available on most platforms. This means that programs that use Checkbutton can be run on a wide range of operating systems.
Overall, Python Tkinter Checkbutton is an important tool for creating user-friendly graphical user interfaces in Python. It provides an easy way for users to interact with a program and for developers to design effective user interfaces.
Here’s a list of all the options that can be used with Python Tkinter Checkbutton:
activebackground:
Background color of the Checkbutton when it is under the mouse pointer.
activeforeground:
Foreground (text) color of the Checkbutton
when it is under the mouse pointer.
anchor:
Determines where the text is positioned within the Checkbutton.
background or bg:
Background color of the Checkbutton.
bd:
Border width in pixels.
command:
A function or method that is called when the Checkbutton is clicked.
cursor:
Cursor to be displayed when the mouse is over the Checkbutton.
disabledforeground:
Foreground (text) color of the Checkbutton when it is disabled.
font:
Font used for the text in the Checkbutton.
foreground or fg:
Foreground (text) color of the Checkbutton.
height:
Height of the Checkbutton in pixels.
highlightbackground:
Color of the Checkbutton’s highlight border when it does not have focus.
highlightcolor:
Color of the Checkbutton’s highlight when it has focus.
highlightthickness:
Thickness of the Checkbutton’s highlight border.
image:
A PhotoImage or Bitmap image displayed in place of the text.
indicatoron:
Set to 0 to remove the box indicator, and 1 to include it.
justify:
Determines how the text is justified within the Checkbutton.
offvalue:
The value associated with the Checkbutton when it is not selected (default is 0).
onvalue:
The value associated with the Checkbutton when it is selected (default is 1).
overrelief:
Set to the relief style of the Checkbutton when the mouse is over it.
padx:
Horizontal padding between the Checkbutton’s text/image and its border.
pady:
Vertical padding between the Checkbutton’s text/image and its border.
relief:
Border style of the Checkbutton.
selectcolor:
Color of the indicator box when the Checkbutton is selected.
selectimage:
A PhotoImage or Bitmap image displayed in place of the indicator box when the Checkbutton is selected.
state:
Set to DISABLED to gray out the Checkbutton and prevent it from being clicked.
takefocus:
Set to 0 to prevent the Checkbutton from receiving keyboard focus.
text:
The label or text displayed next to the Checkbutton.
textvariable:
A variable containing the label or text displayed next to the Checkbutton.
tristateimage:
A PhotoImage or Bitmap image displayed in place of the indicator box when the Checkbutton is in an indeterminate state.
tristatevalue:
The value associated with the Checkbutton when it is in an indeterminate state.
variable:
A Tkinter variable associated with the Checkbutton, used to retrieve the value of the Checkbutton.
width:
Width of the Checkbutton in characters (not pixels).
wraplength:
Maximum number of pixels before text is wrapped to the next line.
Note that not all of these options are applicable in every situation, and some options have default values that may not be explicitly set.
Here are some code examples for each of the options available with the Tkinter Checkbutton:
Background color of the Checkbutton when it is under the mouse pointer.
activeforeground:
Foreground (text) color of the Checkbutton when it is under the mouse pointer.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", activeforeground="white", activebackground="blue") checkbutton.pack() root.mainloop()
anchor:
Determines where the text is positioned within the Checkbutton.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", anchor=W) checkbutton.pack() root.mainloop()
background or bg:
Background color of the Checkbutton.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", bg="green") checkbutton.pack() root.mainloop()
bd:
Border width in pixels.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", bd=40) checkbutton.pack() root.mainloop()
cursor:
Cursor to be displayed when the mouse is over the Checkbutton.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", cursor="hand2") checkbutton.pack() root.mainloop()
note : the shape of cursor become “hand shape” when mouse is over it
font:
Font used for the text in the Checkbutton.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", font=("Helvetica", 16)) checkbutton.pack() root.mainloop()
foreground or fg:
Foreground (text) color of the Checkbutton.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", fg="red") checkbutton.pack() root.mainloop()
Color of the Checkbutton’s highlight border when it does not have focus.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", highlightbackground="red") checkbutton.pack() root.mainloop()
Color of the Checkbutton’s focus highlight border.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", highlightcolor="blue", highlightthickness=2) checkbutton.pack() root.mainloop()
Alignment of the text within the Checkbutton.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", justify=LEFT) checkbutton.pack() root.mainloop()
Value assigned to the Checkbutton when it is unchecked.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", offvalue="unchecked") checkbutton.pack() root.mainloop()
onvalue:
Value assigned to the Checkbutton when it is checked.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", onvalue="checked") checkbutton.pack() root.mainloop()
Padding in pixels added to the left and right of the Checkbutton’s text or image.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", padx=20) checkbutton.pack() root.mainloop()
Padding in pixels added above and below the Checkbutton’s text or image.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", pady=20) checkbutton.pack() root.mainloop()
Border decoration of the Checkbutton.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", relief=SOLID) checkbutton.pack() root.mainloop()
Color of the Checkbutton when it is checked.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", selectcolor="red") checkbutton.pack() root.mainloop()
State of the Checkbutton (normal, active, disabled).
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", state=DISABLED) checkbutton.pack() root.mainloop()
Index of the character in the Checkbutton’s text to underline.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", underline=1) checkbutton.pack() root.mainloop()
Tkinter variable that stores the value of the Checkbutton.
from tkinter import * root = Tk() root.geometry("200x200") var = BooleanVar() checkbutton = Checkbutton(root, text="Check me", variable=var) checkbutton.pack() root.mainloop()
Width of the Checkbutton in characters or pixels.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="Check me", width=20) checkbutton.pack() root.mainloop()
Maximum line length for the Checkbutton’s text before wrapping occurs.
from tkinter import * root = Tk() root.geometry("200x200") checkbutton = Checkbutton(root, text="This is a really long text that should wrap at some point", wraplength=100) checkbutton.pack() root.mainloop()