I am trying to grey out or disable the checkboxes depending on the selection of the radio button. For example if I select the "BRF" radio button I would like the "BRF" checkbox to be disabled but all the other ones enabled and so forth...I am very new to Tkinter and can't figure out why my code is not working.
I have tried setting the state on the condition of the radio selection but it appears not to do anything.
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()
I expect when the radio button parameter on the left is selected that the equal and opposite checkbox would be disabled. Instead all of the checkboxes are enabled regardless of my radio button selection...Can anyone help? I'm totally stuck!
Aucun commentaire:
Enregistrer un commentaire