Python 条件满足后,如何让按钮显示?

Python 条件满足后,如何让按钮显示?,python,tkinter,Python,Tkinter,这就是我的代码目前的样子 sub1 = Tk() sub1.title("Checklist") sub1.geometry("1500x800") def spawnButton(): if finalVar==5: bQuit = Button(sub1, text="You are good to go!", command=destroy).grid(sticky="W", row=

这就是我的代码目前的样子

sub1 = Tk()
sub1.title("Checklist")
sub1.geometry("1500x800")

def spawnButton():
    if finalVar==5:
        bQuit = Button(sub1, text="You are good to go!", command=destroy).grid(sticky="W", row=7, pady=10)

def destroy():
    sub1.destroy()

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
finalVar = var1.get() + var2.get() + var3.get() + var4.get() + var5.get()

ck1 = Label(sub1, text="Here is a checklist to help you determine if you are ready to go out!").grid(sticky="W", row=0)
ck2 = Checkbutton(sub1, text="Face Mask", variable=var1, command=spawnButton).grid(sticky="W", row=1, pady=(30,0))
ck3 = Checkbutton(sub1, text="Hand Sanitizer", variable=var2, command=spawnButton).grid(sticky="W", row=2)
ck4 = Checkbutton(sub1, text="MySejahtera application installed on mobile phone", variable=var3, command=spawnButton).grid(sticky="W", row=3)
ck5 = Checkbutton(sub1, text="Wallet with enough money for expenditures", variable=var4, command=spawnButton).grid(sticky="W", row=4)
ck6 = Checkbutton(sub1, text="Identity Card", variable=var5, command=spawnButton).grid(sticky="W", row=5)
ck7 = Checkbutton(sub1, text="Driving License (if applicable)").grid(sticky="W", row=6)
其思想是,当用户检查所有ck2-ck6(ck7不相关)时,spawnButton()创建一个按钮,然后允许用户单击该按钮并关闭窗口

代码在CMD上运行良好,但是当我检查所有ck2-ck6时,按钮并没有像我希望的那样生成

任何帮助都将不胜感激。谢谢大家!

(归功于@Wups)

为了澄清,这里是更新的代码

from tkinter import *

sub1 = Tk()
sub1.title("Checklist")
sub1.geometry("500x300")

def spawnButton():
    finalVar = var1.get() + var2.get() + var3.get() + var4.get() + var5.get()   # add this line
    if finalVar==5:
        bQuit = Button(sub1, text="You are good to go!", command=destroy).grid(sticky="W", row=7, pady=10)

def destroy():
    sub1.destroy()

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()

ck1 = Label(sub1, text="Here is a checklist to help you determine if you are ready to go out!").grid(sticky="W", row=0)
ck2 = Checkbutton(sub1, text="Face Mask", variable=var1, command=spawnButton).grid(sticky="W", row=1, pady=(30,0))
ck3 = Checkbutton(sub1, text="Hand Sanitizer", variable=var2, command=spawnButton).grid(sticky="W", row=2)
ck4 = Checkbutton(sub1, text="MySejahtera application installed on mobile phone", variable=var3, command=spawnButton).grid(sticky="W", row=3)
ck5 = Checkbutton(sub1, text="Wallet with enough money for expenditures", variable=var4, command=spawnButton).grid(sticky="W", row=4)
ck6 = Checkbutton(sub1, text="Identity Card", variable=var5, command=spawnButton).grid(sticky="W", row=5)
ck7 = Checkbutton(sub1, text="Driving License (if applicable)").grid(sticky="W", row=6)

sub1.mainloop()
输出


该值在开始时分配给
finalVar
一次。你需要把它变成一个函数,或者只是在
spawnButton
@Wups中检查var1到5,那么如果我在if语句之前将finalVar=var1.get()+var2.get()+var3.get()+var4.get()+var5.get()移动到spawnButton中,它会工作吗?我刚试过,但按钮仍然没有生成:/我刚测试过。它适合我。您应该只有一个
Tk()
对象。变量
sub1
看起来不是唯一的变量。如果您需要多个窗口,请使用
Toplevel
小部件。