-1

I'm creating a window in which you have 4 options in a checkbutton (using Tkinter in Python). I wish that, whenever you click one option, the others change state do DISABLED. I saw a post in which they used this:

root = Tk()
def click():
    check.config(state=DISABLED)
check = Checkbutton(text="Click Me", command=click)
check.grid()
root.mainloop()

However, in my program I have several windows, and I am located in the third window. And when I make a def to make the unselected checkbuttons to disable i get an error that says that my checkbuttons are not defined, this is the part of the code that matters:

    budget1 = IntVar()
    budget2 = IntVar()
    budget3 = IntVar()
    budget4 = IntVar()

    window3 = Toplevel()
    window3.config(bg="lightgreen")
    window3.title("My details")
    window3.resizable(1, 1)

    secondary_font=("Arial 20 bold")


    check_3box = Frame(window3, bg="lightgreen")
    check_3box.grid(column=0, row=7)

    W9_W3 = Checkbutton(check_3box, text="$0-$300", variable=budget1, bg="white", fg="black", command=budget_disable(1))
    W9_W3.grid(column=0,row=7, padx=15, pady=1)
    W10_W3 = Checkbutton(check_3box, text="$300-500", variable=budget2, bg="white", fg="black", command=budget_disable(2))
    W10_W3.grid(column=1, row=7, padx=15, pady=1)
    W11_W3 = Checkbutton(check_3box, text="$500-$900", variable=budget3, bg="white", fg="black", command=budget_disable(3))
    W11_W3.grid(column=2, row=7, padx=15, pady=1)
    W12_W3 = Checkbutton(check_3box, text="$900+",variable=budget4, bg="white", fg="black", command=budget_disable(4))
    W12_W3.grid(column=3, row=7, padx=15, pady=1)

And I have this def right at the begining of the code

def budget_disable(a):

    if a == 1:
        W10_W3.config(state=DISABLED)
        W11_W3.config(state=DISABLED)
        W12_W3.config(sate=DISABLED)

    if a == 2:
        W9_W3.config(state=DISABLED)
        W11_W3.config(state=DISABLED)
        W12_W3.config(sate=DISABLED)

    if a == 3:
        W9_W3.config(state=DISABLED)
        W10_W3.config(state=DISABLED)
        W12_W3.config(sate=DISABLED)

    if a == 4:
        W9_W3.config(state=DISABLED)
        W10_W3.config(state=DISABLED)
        W11_W3.config(state=DISABLED)

And the error that appears is

NameError: name 'W10_W3' is not defined
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 1
    It is in a function,so it is a local variable. – jizhihaoSAMA Apr 25 '20 at 23:31
  • Does this answer your question? [how-would-i-access-variables-from-one-class-to-another](https://stackoverflow.com/questions/19993795). Read [why-is-button-parameter-command-executed-when-declared](https://stackoverflow.com/questions/5767228) – stovfl Apr 26 '20 at 17:44

1 Answers1

-1

The solution is to change your checkbutton assignment statements to:

W9_W3 = Checkbutton(check_3box, text="$0-$300", bg="white", fg="black", command= lambda: budget_disable(1))

The command argument should be passed the name of a function ie. budget_disable instead of calling the function ie. budget_disable(), otherwise the function is called when the checkbutton is created and so the function is run before you have made the other checkbuttons. However, if you want to pass an argument to the function (like you do) you can use lambda to create a function that calls your function with an argument.

I hope my explanation makes sense :)

TheFluffDragon9
  • 514
  • 5
  • 11