Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

import tkinter as tk

# Create a function to update the label text when a check button is toggled

def update_label():
selected_options = []
for text, var in zip(option_texts, selected_options_vars):
if var.get():
selected_options.append(text)
selected_option_text.set("Selected options: " + ', '.join(selected_options))

# Create the main Tkinter window


root = tk.Tk()
root.title("Check Buttons Example")

# Create a label to display the selected options


selected_option_text = tk.StringVar()
label = tk.Label(root, textvariable=selected_option_text)
label.pack()

# Create a list to store the selected options as BooleanVars and their corresponding texts
selected_options_vars = []
option_texts = ["Option 1", "Option 2"]

# Create two check buttons


for text in option_texts:
var = tk.BooleanVar()
selected_options_vars.append(var)
check_button = tk.Checkbutton(root, text=text, variable=var, command=update_label)
check_button.pack()

# Start the Tkinter main loop


root.mainloop()

You might also like