代码之家  ›  专栏  ›  技术社区  ›  ankit kumar

python脚本在x秒后运行,但当插入tkinter代码时,它只运行一次

  •  1
  • ankit kumar  · 技术社区  · 6 年前

    What is the best way to repeatedly execute a function every x seconds in Python?

    我已经尝试了上述链接上张贴的解决方案,但没有帮助我达到预期的结果。

    上面的代码在控制台上按提到的秒数多次打印“Doing stuff…”,即5,但是当我添加window()行时,这是一个用于显示消息的tkinter代码,代码只运行一次,而不是任何时候再次运行。

    请帮忙。我想根据系统时钟在特定时间反复运行tkinter代码,但现在我只想在x秒后执行它。

    1 回复  |  直到 6 年前
        1
  •  0
  •   1966bc    6 年前

    我已经设定了时间。在课堂上每秒钟睡一次觉。

    在这件事上你有两个好处,第一,随时重复你的职能

    import tkinter as tk
    import threading
    import queue
    import datetime
    import time
    
    class MyThread(threading.Thread):
    
        def __init__(self, queue,):
            threading.Thread.__init__(self)
    
            self.queue = queue
            self.check = True
    
        def stop(self):
            self.check = False
    
        def run(self):
    
    
            while self.check:
                x = "Doing stuff.. "
                y = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                msg = x+y
                time.sleep(1)
                self.queue.put(msg)
    
    
    class App(tk.Frame):
    
        def __init__(self,):
    
            super().__init__()
    
            self.master.title("Hello World")
            self.master.protocol("WM_DELETE_WINDOW",self.on_close)
    
            self.queue = queue.Queue()
    
            self.my_thread = None
    
            self.init_ui()
    
        def init_ui(self):
    
            self.f = tk.Frame()
    
            w = tk.Frame()
    
            tk.Button(w, text="Start", command=self.launch_thread).pack()
            tk.Button(w, text="Stop", command=self.stop_thread).pack()
            tk.Button(w, text="Close", command=self.on_close).pack()
    
            w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
            self.f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
    
        def launch_thread(self):
    
            if (threading.active_count()!=0):
    
                self.my_thread = MyThread(self.queue)
                self.my_thread.start()
                self.periodiccall()
    
        def stop_thread(self):
    
         if(threading.active_count()!=1):
             self.my_thread.stop()
    
    
        def periodiccall(self):
    
            self.checkqueue()
            if self.my_thread.is_alive():
                self.after(1, self.periodiccall)
            else:
                pass
    
        def checkqueue(self):
            while self.queue.qsize():
                try:
                    ret = self.queue.get(0)
                    msg = "%s"%(ret)
                    print(msg)
                except queue.Empty:
                    pass                    
    
        def on_close(self):
            if(threading.active_count()!=1):
                self.my_thread.stop()
    
            self.master.destroy()
    
    if __name__ == '__main__':
        app = App()
        app.mainloop()
    
        2
  •  1
  •   NemoMeMeliorEst    6 年前

    这将允许您每x秒运行一个命令。

    tkinter代码只运行一次的问题是,因为它首先被设置,然后进入一个循环(root.mainloop()),因此再也不会返回到代码来显示任何内容。

    例子: tkinter: how to use after method