首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >启用和禁用多个复选框CustomTkinter

启用和禁用多个复选框CustomTkinter
EN

Stack Overflow用户
提问于 2022-10-27 11:18:26
回答 1查看 65关注 0票数 0

这可能是个问题。

但我一直在绞尽脑汁,试图启用基于无线电按钮的复选框。

我所做的是基于数组(即dc_ma1 )创建一个多个复选框,并使其进入禁用状态。单选按钮将根据用户选择启用或禁用复选框。

下面是一些代码:

代码语言:javascript
复制
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()

当我尝试启用复选框时,它只启用最后一个复选框。我做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-28 03:57:46

补充一下,这是基于这里的注释的工作代码。正如@a 1668所强调的那样,我需要使用列表或字典来存储复选框的引用:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74221228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档