python - Tkinter 应用程序在尝试重新启动时冻结
问题描述
所以,我正在尝试使用 tkinter 制作一个计时器。到目前为止,代码本身有效,我得到了弹出窗口,但是如果我再次尝试单击“开始”,窗口就会冻结。我不知道发生了什么,因为它昨天没有这样做。任何反馈表示赞赏。提前致谢!
from tkinter import *
from tkinter import messagebox
import time
import os
os.system("clear")
#functions
def popup():
time.sleep(1200) #20minutes of delay
response = messagebox.showinfo("Time to rest!", "Look through the window to rest your eyes.")
#Label(root, text=response).pack()
#root.after(2000, popup)
#tkinter GUI
root = Tk()
root.title("Eye timer")
root.geometry("400x200")
myTitle = Label(root, text="A popup will tell you to rest your eyes,"
"\n20 minutes after you click the start button.")
myTitle.pack(pady=10)
start = Button(root, text="Start", command=popup)
start.pack(pady=10)
root.mainloop()
解决方案
程序冻结 20 分钟。
修复:
import threading
start = Button(root, text="Start", command=threading.Thread(target=popup).start())```
推荐阅读
- gams-math - 如何在向量(参数)中找到?
- javascript - 如何将数据保存在本地存储中?
- c# - 谷歌日历 API 定期事件(按小时)
- html - 为什么当浏览器大小改变时我的列不调整大小
- python - 将计划信息(开始和结束时间)拆分为更小的间隔
- asp.net-mvc - 单击按钮时未选择 Casecading 下拉列表
- node.js - 为什么我会收到此错误:SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (
) 在 XMLHtt…, - geometry - In SwiftUI, how to determine a Circle()'s size to draw a overlay on scale with that circle?
- angular - 在 Angular 6/7 中获取 URL 参数的最佳方法
- javascript - 禁用 iFrame 内的外部内容加载