【发布时间】:2021-05-12 00:29:30
【问题描述】:
我目前遇到一个问题,我试图在 tkinter 窗口内运行一个 while 循环。它一直等到 while 循环完成才能实际显示窗口。我想要发生的是窗口出现,然后 while 循环将开始。经过研究发现跟root.mainloop()函数有关,但不知道怎么改。
#creates new window
root = Tk()
#makes backdrop picture of map
C = Canvas(root, bg="black", height=780, width=1347)
C.grid(row=0,column=0)
filename = PhotoImage(file = "map4.png")
background_label = Label(root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
totalDeaths = 0
totalCases = 0
totalPopulation = 15000
dayCount = 0
#loops until total deaths = population
while totalDeaths < totalPopulation:
totalDeaths += 1
time.sleep(1)
root.mainloop()
【问题讨论】:
-
while 循环需要 15000 秒(4 小时多一点)才能完成。
-
我刚刚这样做是为了使代码更易于阅读。有一个复杂的函数,我已将其替换为 totalDeaths+= 1
-
我们只能看到您在问题中输入的代码。我们无法知道您的计算机上有什么代码。您发布的代码休眠了四个多小时。
标签: python python-3.x loops tkinter while-loop