Cover Image for Python Tkinter Checkbutton
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:

  1. We import the tkinter module and create a main window using tk.Tk().
  2. We create a list selected_options to hold the selected options from the Checkbuttons.
  3. 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 the selected_options list.
  4. We create three Checkbutton widgets (checkbutton1, checkbutton2, and checkbutton3) with text labels and use the command parameter to associate each Checkbutton with a specific option and the on_checkbox_click function.
  5. We create a Label widget named selected_label to display the selected options.
  6. We define a function update_selected_label() to update the text of the selected_label with the current selected options.
  7. We create a button named update_button with the text “Update Selected” and associate it with the update_selected_label function.
  8. We use the pack() method to add the Checkbuttons, selected options label, and update button to the main window.
  9. 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.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS