Home > Back-end >  Enabled and disabled multiple checkboxes CustomTkinter
Enabled and disabled multiple checkboxes CustomTkinter

Time:11-04

This might be a noob question.

But I have been raking my head on trying to enable the checkbox based on the radiobutton.

What I did is create a multiple checkboxes based on the array which is dc_ma1 and make it into the disable state. The radio button will enable or disable the checkboxes based on the user choice.

Below are some of the code:

from tkinter import *
import customtkinter

customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"
root = customtkinter.CTk()   

root.minsize(300, 300) 
dc_ma1 = ["7190", "7189", "6264", "6262"]
dc_ma1_l=[str(i) for i in range(len(dc_ma1))]
print (dc_ma1_l)
selected_dc=[]

root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=0)
frame_lower_left = customtkinter.CTkFrame(master=root, corner_radius= 0)
frame_lower_left.grid(row=1, column=0, sticky="nswe", padx=20, pady=20)

frame_lower_left.rowconfigure((0, 1, 3 , 4), weight=0)
frame_lower_left.columnconfigure((0, 1, 2, 3, 4, 5), weight=1)

def cb_state():
    print (str(radio_CB.get()))

    if radio_CB.get() == 1:
        print("radio is 1")
        for x in range(len(dc_ma1)):
            #print(dc_ma1)
            check_CB1.configure(state="normal")     
    else:
        print("radio is 0")    

frame_radio = customtkinter.CTkFrame(master=frame_lower_left)
frame_radio.grid(row=0, column=0, columnspan=6, pady=20, padx=20, sticky="nsew")
frame_cb = customtkinter.CTkFrame(master= frame_lower_left)
frame_cb.grid(row=1, column=0, columnspan = 6, pady=20, padx=20, sticky="nsew")
frame_cb.pack_forget()

frame_cb.grid_columnconfigure(4, minsize=10)   # empty row with minsize as spacing
frame_cb.columnconfigure(4, weight=1)


# radio button to Disable and disable CB
radio_CB = IntVar(value=0)
label_radio_group = customtkinter.CTkLabel(master=frame_radio, text="Combiner Box Configuration:")
label_radio_group.grid(row=0, column=0, pady=10, padx=10, sticky="w", columnspan = 5)

radio_button_1 = customtkinter.CTkRadioButton(master=frame_radio, variable=radio_CB, value=0, command=cb_state, text= "Disable Checkbox")
radio_button_1.grid(row=0, column=10, pady=20, padx=10, sticky="w", columnspan = 5)
radio_button_2 = customtkinter.CTkRadioButton(master=frame_radio, variable=radio_CB, value=1, command=cb_state, text="Enable Checkbox")
radio_button_2.grid(row=0, column=20, pady=20, padx=10, sticky="w", columnspan = 5)

for x in range(len(dc_ma1)):
    dc_ma1_l[x]=IntVar(0)
    #print(dc_ma1_l)
    y=dc_ma1_l[x]
            
    check_CB1 = customtkinter.CTkCheckBox(master= frame_cb, text="CB "   str(x 1), variable=dc_ma1_l[x],offvalue=0,onvalue=1, command=lambda x=dc_ma1[x],y=dc_ma1_l[x]:add_remove(x,y))
    check_CB1.grid(row=2, column=x, pady=10, padx=10, sticky="nsew")
    check_CB1.configure(state="disabled")

root.mainloop()

When I tried to Enable the checkbox it only enable the last checkbox. What did I do wrong?

CodePudding user response:

Just to add, this the working code based on the comment here. As per highlighted by @acw1668 I need to to use a list or dictionary to store those references of checkboxes:

from tkinter import *
import customtkinter

customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"
root = customtkinter.CTk()   

root.minsize(300, 300) 
dc_ma1 = ["7190", "7189", "6264", "6262"]
dc_ma1_l=[str(i) for i in range(len(dc_ma1))]
print (dc_ma1_l)
selected_dc=[]

root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=0)
frame_lower_left = customtkinter.CTkFrame(master=root, corner_radius= 0)
frame_lower_left.grid(row=1, column=0, sticky="nswe", padx=20, pady=20)

frame_lower_left.rowconfigure((0, 1, 3 , 4), weight=0)
frame_lower_left.columnconfigure((0, 1, 2, 3, 4, 5), weight=1)

def cb_state():
    print (str(radio_CB.get()))

    if radio_CB.get() == 1:
        print("radio is 1")
        for x in range(len(dc_ma1)):
            #print(dc_ma1)
            check_CB1[x].configure(state="normal")     
    else:
        print("radio is 0")    
        for x in range(len(dc_ma1)):
            #print(dc_ma1)
            check_CB1[x].configure(state="disabled")
            
frame_radio = customtkinter.CTkFrame(master=frame_lower_left)
frame_radio.grid(row=0, column=0, columnspan=6, pady=20, padx=20, sticky="nsew")
frame_cb = customtkinter.CTkFrame(master= frame_lower_left)
frame_cb.grid(row=1, column=0, columnspan = 6, pady=20, padx=20, sticky="nsew")
frame_cb.pack_forget()

frame_cb.grid_columnconfigure(4, minsize=10)   # empty row with minsize as spacing
frame_cb.columnconfigure(4, weight=1)


# radio button to Disable and disable CB
radio_CB = IntVar(value=0)
label_radio_group = customtkinter.CTkLabel(master=frame_radio, text="Combiner Box Configuration:")
label_radio_group.grid(row=0, column=0, pady=10, padx=10, sticky="w", columnspan = 5)

radio_button_1 = customtkinter.CTkRadioButton(master=frame_radio, variable=radio_CB, value=0, command=cb_state, text= "Disable Checkbox")
radio_button_1.grid(row=0, column=10, pady=20, padx=10, sticky="w", columnspan = 5)
radio_button_2 = customtkinter.CTkRadioButton(master=frame_radio, variable=radio_CB, value=1, command=cb_state, text="Enable Checkbox")
radio_button_2.grid(row=0, column=20, pady=20, padx=10, sticky="w", columnspan = 5)


check_CB1 = {} 

for x in range(len(dc_ma1)):
    dc_ma1_l[x]=IntVar(0)
    #print(dc_ma1_l)
    
    y=dc_ma1_l[x]
            
    check_CB1[x] = customtkinter.CTkCheckBox(master= frame_cb, text="CB "   str(x 1), offvalue=0,onvalue=1, command=lambda x=dc_ma1[x],y=dc_ma1_l[x]:add_remove(x,y))
    check_CB1[x].grid(row=2, column=x, pady=10, padx=10, sticky="nsew")
    check_CB1[x].configure(state="disabled")

root.mainloop()
  • Related