首页 > 解决方案 > 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()

标签: pythontkinter

解决方案


程序冻结 20 分钟。

修复:

import threading

start = Button(root, text="Start", command=threading.Thread(target=popup).start())```

推荐阅读