python – 每隔x分钟创建一个tkinter窗口,然后在y秒后自动关闭它

我正在尝试构建一个简单的程序来提醒我在使用计算机时休息一下.我对
python有一个合理的理解,但以前从未玩过GUI编程或线程,所以以下基本上是从stackoverflow复制/粘贴:

import threading
import time
import Tkinter

class RepeatEvery(threading.Thread):
    def __init__(self, interval, func, *args, **kwargs):
        threading.Thread.__init__(self)
        self.interval = interval  # seconds between calls
        self.func = func          # function to call
        self.args = args          # optional positional argument(s) for call
        self.kwargs = kwargs      # optional keyword argument(s) for call
        self.runable = True

    def run(self):
        while self.runable:
            self.func(*self.args, **self.kwargs)
            time.sleep(self.interval)

    def stop(self):
        self.runable = False

def microbreak():
    root = Tkinter.Tk()
    Tkinter.Frame(root, width=250, height=100).pack()
    Tkinter.Label(root, text='Hello').place(x=10, y=10)
    threading.Timer(3.0, root.destroy).start()
    root.mainloop()
    return()

thread = RepeatEvery(6, microbreak)
thread.start()

这给了我第一次中断通知但在给我第二次中断通知之前失败了.

Tcl_AsyncDelete: async handler deleted by the wrong thread

fish: Job 1, “python Documents/python/timer/timer.py ” terminated by
signal SIGABRT (Abort)

有任何想法吗?我很乐意使用除了tkinter之外的其他东西用于gui-stuff或者除了线程以外的东西来实现时间的东西.

根据以下答案,我的新工作脚本如下:

import Tkinter as Tk
import time

class Window:
    def __init__(self):
        self.root = None
        self.hide = 10 #minutes
        self.show = 10 #seconds

    def close(self):
        self.root.destroy()
        return

    def new(self):
        self.root = Tk.Tk()
        self.root.overrideredirect(True)
        self.root.geometry("{0}x{1}+0+0".format(self.root.winfo_screenwidth(), self.root.winfo_screenheight()))
        self.root.configure(bg='black')
        Tk.Label(self.root, text='Hello', fg='white', bg='black', font=('Helvetica', 30)).place(anchor='center', relx=0.5, rely=0.5)
        #Tk.Button(text = 'click to end', command = self.close).pack()
        self.root.after(self.show*1000, self.loop)

    def loop(self):
        if self.root:
            self.root.destroy()
        time.sleep(self.hide*60)
        self.new()
        self.root.mainloop()
        return

Window().loop()

最佳答案 我认为没有线程就可以更轻松地实现这一点,而Tkinter并没有很好地集成.相反,您可以使用after和after_idle方法来安排在特定超时后运行的回调.您可以创建一个显示窗口的方法并将其计划为隐藏窗口,另一个方法隐藏窗口并安排窗口显示.然后他们只会在无限循环中互相称呼:

import Tkinter

class Reminder(object):
    def __init__(self, show_interval=3, hide_interval=6):
        self.hide_int = hide_interval  # In seconds
        self.show_int = show_interval  # In seconds
        self.root = Tkinter.Tk()
        Tkinter.Frame(self.root, width=250, height=100).pack()
        Tkinter.Label(self.root, text='Hello').place(x=10, y=10)
        self.root.after_idle(self.show)  # Schedules self.show() to be called when the mainloop starts

    def hide(self):
        self.root.withdraw()  # Hide the window
        self.root.after(1000 * self.hide_int, self.show) # Schedule self.show() in hide_int seconds

    def show(self):
        self.root.deiconify() # Show the window
        self.root.after(1000 * self.show_int, self.hide)  # Schedule self.hide in show_int seconds

    def start(self):
        self.root.mainloop()

if __name__ == "__main__":
    r = Reminder()
    r.start()
点赞