简体   繁体   中英

Disabling Checkbutton from secondary window - Tkinter

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

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM