0
votes

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!

1
When you select any Radiobutton, selected1() function is called which prints the button name, but the state of Checkboxes is decided only once when creating checkboxes, so you need to change the state with config method of checkbox in selected1() method. - Kamal
Thanks for your answer Kamal, would you be able to show an example of what you mean with a small piece of code? - Jordan Gallacher
@JordanGallacher: brf.configure(state = ['disabled']) - stovfl
def selected1(): print(var1.get()) if var1.get() == "BRF": print("TRUE") brf.configure(state = ['disabled']) else: print("FALSE") brf.configure(state = ['enabled']) - Jordan Gallacher
I created this definition but when I call it I get: TclError: invalid command name ".!checkbutton3" - Jordan Gallacher

1 Answers

0
votes

You need to change the state of required checkboxes inside selected1() as below:

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)

And the if/elif blocks before creating the checkboxes are not necessary and should be removed. Of course you need to remove all state=... as well when creating the checkboxes after removing the if/elif blocks. Also variable=tk.IntVar() used in creating the checkboxes is not necessary because you cannot refer to those IntVar. Below is the modified codes to create the checkboxes:

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)