我正在尝试根据单选按钮的选择将其涂灰或禁用复选框。例如,如果我选择“ BRF” 单选按钮,我希望禁用“ BRF”复选框,但同时启用所有其他功能,依此类推...我 Tkinter 的新手,无法弄清楚我的代码为什么不起作用。
我尝试根据无线电选择的条件设置状态,但似乎无能为力。
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
window.configure(background='white')
window.geometry("500x600") # This sets the Window size to work with
window.title('Please select scan options')
button = tk.Button(window, font="Heltavica",text ="PROCEED", command=window.destroy)
button.config(bd=8, font="Ariel", justify="center")
button.pack(side=tk.BOTTOM)
comp = [('BRF','Orange'), ('SHG','Green') ,('THG','Purple'), ('ETA','Blue'), ('MAIN TEC', 'Red')]
power_scan = tk.IntVar()
check1 = tk.Checkbutton(window, text='Get power',
command=power_scan.get, variable = power_scan ,
onvalue=1, offvalue=0)
check1.place(x=400,y=50)
noise_scan = tk.IntVar()
check2 = tk.Checkbutton(window, text='Get noise',
command=noise_scan.get(), variable = noise_scan ,
onvalue=1, offvalue=0)
check2.place(x=400,y=100)
if power_scan.get():
# do something
print("Power scan selected")
if noise_scan.get():
# do something
print("Noise scan selected")
tk.Label(window,
text="""Please select a parameter scan :""",
justify = tk.LEFT,
padx = 20).pack()
def disable_button():
print('disable button')
button.config(state=tk.DISABLED)
def enable_button():
print('enable button')
button.config(state=tk.NORMAL)
def changebutton():
print('changebutton=', var1.get())
if var1.get()==1:
enable_button()
else:
disable_button()
def selected1():
print(var1.get())
def selected2():
print(var2.get())
var1 = tk.StringVar() #used to get the 'value' property of a tkinter.Radiobutton
var2 = tk.IntVar()
components = [("BRF", "BRF"),
("SHG", "SHG"),
("THG", "THG"),
("ETA", "ETA"),
("MAIN TEC", "MAIN TEC")
]
count = 0
for text, mode in components :
a = tk.Radiobutton(window, text=text,
variable=var1, value=mode, command = selected1,indicatoron = 0)
a.place(x=130,y=40+15*count*2)
count += 1
if selected1() == None:
brfstate='enabled'
shgstate='enabled'
thgstate ='enabled'
etastate='enabled'
maintstate='enabled'
if selected1() == "BRF":
brfstate='disabled'
shgstate='enabled'
thgstate ='enabled'
etastate='enabled'
maintstate='enabled'
elif selected1() == "SHG":
brfstate='enabled'
shgstate='disabled'
thgstate ='enabled'
etastate='enabled'
maintstate='enabled'
elif selected1() == "THG":
brfstate='enabled'
shgstate='enabled'
thgstate ='disabled'
etastate='enabled'
maintstate='enabled'
elif selected1() == "ETA":
brfstate='enabled'
shgstate='enabled'
thgstate ='enabled'
etastate='disabled'
maintstate='enabled'
elif selected1() == "MAIN TEC":
brfstate='enabled'
shgstate='enabled'
thgstate ='enabled'
etastate='enabled'
maintstate='disabled'
brf = ttk.Checkbutton(window, text="BRF", variable=tk.IntVar(), state=brfstate, onvalue=1, offvalue=0)
brf.place(x=300,y=40+15*0*2)
shg = ttk.Checkbutton(window, text="SHG", variable=tk.IntVar(), state=shgstate, onvalue=1, offvalue=0)
shg.place(x=300,y=40+15*1*2)
thg = ttk.Checkbutton(window, text="THG", variable=tk.IntVar(), state=thgstate, onvalue=1, offvalue=0)
thg.place(x=300,y=40+15*2*2)
eta = ttk.Checkbutton(window, text="ETA", variable=tk.IntVar(), state=etastate, onvalue=1, offvalue=0)
eta.place(x=300,y=40+15*3*2)
maint = ttk.Checkbutton(window, text="MAIN TEC", variable=tk.IntVar(), state=maintstate, onvalue=1, offvalue=0)
maint.place(x=300,y=40+15*4*2)
window.mainloop()
我希望当选择左侧的单选按钮参数时,等号和反号复选框将被禁用。取而代之的是,无论我的单选按钮处于选中状态,所有复选框都被启用。我完全被卡住了!
答案 0 :(得分:0)
您需要按如下所示更改selected1()
中所需复选框的状态:
def selected1():
selected = var1.get()
print(selected)
brf.config(state=tk.DISABLED if selected == "BRF" else tk.NORMAL)
shg.config(state=tk.DISABLED if selected == "SHG" else tk.NORMAL)
thg.config(state=tk.DISABLED if selected == "THG" else tk.NORMAL)
eta.config(state=tk.DISABLED if selected == "ETA" else tk.NORMAL)
maint.config(state=tk.DISABLED if selected == "MAIN TEC" else tk.NORMAL)
在创建复选框之前,if/elif
块不是必需的,应将其删除。当然,在删除state=...
块之后创建复选框时,也需要删除所有if/elif
。同样,创建复选框也不需要使用variable=tk.IntVar()
,因为您不能引用那些IntVar
。下面是修改后的代码,用于创建复选框:
brf = ttk.Checkbutton(window, text="BRF")
brf.place(x=300, y=40+15*0*2)
shg = ttk.Checkbutton(window, text="SHG")
shg.place(x=300, y=40+15*1*2)
thg = ttk.Checkbutton(window, text="THG")
thg.place(x=300, y=40+15*2*2)
eta = ttk.Checkbutton(window, text="ETA")
eta.place(x=300, y=40+15*3*2)
maint = ttk.Checkbutton(window, text="MAIN TEC")
maint.place(x=300, y=40+15*4*2)