
124 views
Python Tkinter Checkbutton
The Python’s Tkinter library, you can create Checkbuttons using the Checkbutton
widget. Checkbuttons are used to allow users to select one or more options from a list of choices. Here’s how you can create a simple Checkbutton using Tkinter:
Python
import tkinter as tk
# Create a main window
root = tk.Tk()
root.title("Tkinter Checkbutton Example")
# Create a list to hold the selected options
selected_options = []
# Function to be called when a Checkbutton is clicked
def on_checkbox_click(option):
if option in selected_options:
selected_options.remove(option)
else:
selected_options.append(option)
# Create Checkbutton widgets
checkbutton1 = tk.Checkbutton(root, text="Option 1", command=lambda: on_checkbox_click("Option 1"))
checkbutton2 = tk.Checkbutton(root, text="Option 2", command=lambda: on_checkbox_click("Option 2"))
checkbutton3 = tk.Checkbutton(root, text="Option 3", command=lambda: on_checkbox_click("Option 3"))
# Create a Label widget to display selected options
selected_label = tk.Label(root, text="Selected Options: ")
# Function to update the selected options label
def update_selected_label():
selected_label.config(text="Selected Options: " + ", ".join(selected_options))
# Create a button to update the selected options label
update_button = tk.Button(root, text="Update Selected", command=update_selected_label)
# Pack the Checkbuttons, selected options label, and update button
checkbutton1.pack()
checkbutton2.pack()
checkbutton3.pack()
selected_label.pack()
update_button.pack()
# Start the Tkinter main loop
root.mainloop()
In this example:
- We import the
tkinter
module and create a main window usingtk.Tk()
. - We create a list
selected_options
to hold the selected options from the Checkbuttons. - We define a function
on_checkbox_click(option)
that will be called when a Checkbutton is clicked. This function toggles the selection state of the Checkbutton’s corresponding option in theselected_options
list. - We create three
Checkbutton
widgets (checkbutton1
,checkbutton2
, andcheckbutton3
) with text labels and use thecommand
parameter to associate each Checkbutton with a specific option and theon_checkbox_click
function. - We create a
Label
widget namedselected_label
to display the selected options. - We define a function
update_selected_label()
to update the text of theselected_label
with the current selected options. - We create a button named
update_button
with the text “Update Selected” and associate it with theupdate_selected_label
function. - We use the
pack()
method to add the Checkbuttons, selected options label, and update button to the main window. - Finally, we start the Tkinter main loop with
root.mainloop()
to display the GUI and handle user interactions.
When you run this code, it will create a window with three Checkbuttons labeled “Option 1,” “Option 2,” and “Option 3.” Users can select or deselect these options by clicking the Checkbuttons. When the “Update Selected” button is clicked, the selected options will be displayed in the label.