Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
The Checkbutton
widget in tkinter
is used to display a number of options to a user as toggle buttons. The user can then select one or more options by ticking the checkboxes.
Let's create a tutorial on how to use the Checkbutton
widget in tkinter
:
tkinter
Tutorial:1. Import Required Libraries:
import tkinter as tk
2. Basic Checkbutton Creation:
To create a basic checkbutton, simply use:
check_btn = tk.Checkbutton(root, text="Option 1") check_btn.pack()
3. Using Variables with Checkbuttons:
To track the state of a checkbutton (whether it's checked or not), you typically use a tk.IntVar()
:
check_var = tk.IntVar() check_btn = tk.Checkbutton(root, text="Option 1", variable=check_var) check_btn.pack()
With the above setup, check_var.get()
will return 1
if the checkbox is checked and 0
otherwise.
4. Using Commands with Checkbuttons:
You can also execute a function when a checkbutton is toggled:
def on_toggle(): if check_var.get(): print("Checked!") else: print("Unchecked!") check_btn = tk.Checkbutton(root, text="Option 1", variable=check_var, command=on_toggle) check_btn.pack()
5. Complete Code Example:
Let's create a GUI where there are multiple checkbuttons and a button to show which options are selected:
import tkinter as tk def display_selected_options(): selected = [] if option1_var.get(): selected.append("Option 1") if option2_var.get(): selected.append("Option 2") if option3_var.get(): selected.append("Option 3") result_label.config(text="Selected: " + ", ".join(selected)) root = tk.Tk() root.title("Checkbutton Tutorial") option1_var = tk.IntVar() option2_var = tk.IntVar() option3_var = tk.IntVar() check_btn1 = tk.Checkbutton(root, text="Option 1", variable=option1_var) check_btn1.pack(anchor=tk.W, padx=20, pady=5) check_btn2 = tk.Checkbutton(root, text="Option 2", variable=option2_var) check_btn2.pack(anchor=tk.W, padx=20, pady=5) check_btn3 = tk.Checkbutton(root, text="Option 3", variable=option3_var) check_btn3.pack(anchor=tk.W, padx=20, pady=5) show_btn = tk.Button(root, text="Show Selected Options", command=display_selected_options) show_btn.pack(pady=20) result_label = tk.Label(root, text="Selected: None") result_label.pack(pady=20) root.mainloop()
In this example, there are three checkbuttons. Clicking the "Show Selected Options" button will display which options are currently checked. This information is updated in a label below the button.
Python Tkinter create Checkbutton example:
import tkinter as tk def on_checkbox_click(): print("Checkbutton clicked") root = tk.Tk() checkbox = tk.Checkbutton(root, text="Enable Feature", command=on_checkbox_click) checkbox.pack() root.mainloop()
How to use Checkbutton widget in Tkinter:
Checkbutton
widget in Tkinter is used to create checkboxes. It can be associated with a variable to track its state.import tkinter as tk root = tk.Tk() # Variable to track the state of the checkbox var = tk.IntVar() checkbox = tk.Checkbutton(root, text="Enable Feature", variable=var) checkbox.pack() root.mainloop()
Creating checkboxes with Checkbutton in Tkinter:
Checkbutton
widget. Each checkbox can be associated with a different variable.import tkinter as tk root = tk.Tk() var1 = tk.IntVar() var2 = tk.IntVar() checkbox1 = tk.Checkbutton(root, text="Option 1", variable=var1) checkbox2 = tk.Checkbutton(root, text="Option 2", variable=var2) checkbox1.pack() checkbox2.pack() root.mainloop()
Python Tkinter Checkbutton variable:
Checkbutton
holds the state of the checkbox (0 for unchecked, 1 for checked).import tkinter as tk root = tk.Tk() var = tk.IntVar() checkbox = tk.Checkbutton(root, text="Enable Feature", variable=var) checkbox.pack() # Accessing the state of the checkbox state = var.get() print("Checkbox state:", state) root.mainloop()
Checkbutton widget events and callbacks in Tkinter:
command
option.import tkinter as tk def on_checkbox_click(): print("Checkbutton clicked") root = tk.Tk() checkbox = tk.Checkbutton(root, text="Enable Feature", command=on_checkbox_click) checkbox.pack() root.mainloop()
Customizing Checkbutton appearance in Tkinter:
Checkbutton
can be customized using options like font, background color, foreground color, etc.import tkinter as tk root = tk.Tk() checkbox = tk.Checkbutton(root, text="Customize", font=("Arial", 12), bg="lightgray", fg="blue") checkbox.pack() root.mainloop()
Handling Checkbutton state changes in Tkinter:
Checkbutton
by associating a callback function with the command
option.import tkinter as tk def on_checkbox_change(): state = var.get() print("Checkbox state:", state) root = tk.Tk() var = tk.IntVar() checkbox = tk.Checkbutton(root, text="Enable Feature", variable=var, command=on_checkbox_change) checkbox.pack() root.mainloop()
Building a form with Tkinter Checkbuttons:
import tkinter as tk def submit_form(): selected_options = [var1.get(), var2.get()] print("Selected options:", selected_options) root = tk.Tk() var1 = tk.IntVar() var2 = tk.IntVar() checkbox1 = tk.Checkbutton(root, text="Option 1", variable=var1) checkbox2 = tk.Checkbutton(root, text="Option 2", variable=var2) submit_button = tk.Button(root, text="Submit", command=submit_form) checkbox1.pack() checkbox2.pack() submit_button.pack() root.mainloop()