0

I am trying to create an app with a pop-up with list of names and the user can select those that are on/off. When they click the "Get Switches Status" button, I want to print a list of the switches that were on.

In the code I wrote only seems to print the initial state of the switches and nothing happens when I click the button. Does anyone have an idea why?

Cheers in advance!

import customtkinter as ctk

def get_switches_status(switches):
    # Get the status of the switches (i.e., which ones are on/off)
    status = [switch.get() for switch in switches]
    
    # Find the indices of the switches that are on
    on_indices = [i for i, val in enumerate(status) if val]
    
    # Print out the status of the switches
    if on_indices:
        print(f"The switches at indices {on_indices} are ON.")
    else:
        print("No switches are ON.")
        
app = ctk.CTk()
switches_gui = []
for i in range(5):
    switch = ctk.CTkSwitch(app, text=f"Switch {i+1}")
    switch.pack()
    switches_gui.append(switch)   

# Create a button to get the status of the switches
get_status_button = ctk.CTkButton(app, text="Get Switches Status", command=get_switches_status(switches = switches_gui))
get_status_button.pack()
# Run the CTk event loop
app.mainloop()



I tried the code above and didn't work. The button works if doesn't require an input and only ouputs something in the "get_switch_status" fucntion. Somethink like:

def get_switches_status():
    print('test')
    
app = ctk.CTk()
get_status_button = ctk.CTkButton(app, text="Get Switches Status", command=get_switches_status)
get_status_button.pack()
app.mainloop()

but this defeats the point of what I am trying to do

Bas
  • 358
  • 3
  • 7

0 Answers0