python - 满足条件后如何让我的按钮显示?
问题描述
这就是我的代码目前的样子
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()
输出
推荐阅读
- android - 应用报告 Google Analytics 和 Firebase:使用定义
- android - 安卓UI组件设计
- angular - @Viewchild 元素上的单击方法不起作用
- javascript - Angular js post无法序列化对象
- django - 仅在本地服务器中编写供个人使用的 Django 应用程序时的数据库注意事项
- python - PySpark - 为组创建特定的时间序列范围
- android - 在flutter中使用google auth的逻辑(一个用户只能从一个gmail和同一设备登录)?
- sql - SQL Pivot 带有值列表的多列
- python - Tensorflow 无法容纳图像列表
- python - 烧瓶`RuntimeError:在应用程序上下文之外工作。`来自中间件