Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python脚本在x秒后运行,但当插入tkinter代码时,它只运行一次_Python_Tkinter - Fatal编程技术网

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

python脚本在x秒后运行,但当插入tkinter代码时,它只运行一次,python,tkinter,Python,Tkinter,我已经尝试了以上链接上发布的解决方案,但都没有帮助我达到预期的效果 上面的代码在控制台上按提到的秒数多次打印“Doing stuff…”,即5,但当我添加window()行时,这是一个用于显示消息的tkinter代码,代码只运行一次,而不是任何时候再次运行 请帮忙。我想根据系统时钟在特定时间反复运行tkinter代码,但现在我只想在x秒后执行它 任何帮助对我都意义重大。谢谢搜索tkinter.after方法 这将允许您每x秒运行一次命令 tkinter代码只运行一次的问题是,因为它首先被设置,然

我已经尝试了以上链接上发布的解决方案,但都没有帮助我达到预期的效果

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

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


任何帮助对我都意义重大。谢谢搜索tkinter.after方法

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

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


示例:

我认为您需要线程和队列……让我来演示一下

我已经设定了时间。在thead类中每秒钟睡眠(1)

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

你渴望和支持你的程序,而不是自己冻结它

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()