首页 > 解决方案 > 满足条件后如何让我的按钮显示?

问题描述

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

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 时,按钮并没有按我的意愿生成。

任何帮助,将不胜感激。谢谢!

标签: pythontkinter

解决方案


(归功于@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()

输出

清单


推荐阅读