首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于Python gui,在不干扰其他函数的情况下,每隔几秒钟重复执行一个函数的最好方法是什么?

对于Python gui,在不干扰其他函数的情况下,每隔几秒钟重复执行一个函数的最好方法是什么?
EN

Stack Overflow用户
提问于 2019-03-24 08:45:07
回答 1查看 84关注 0票数 0

我想重复运行一个函数,该函数每隔60秒从API检索一次数据。然而,我已经意识到我不能在不中断其他函数的情况下让它休眠,或者使用while循环。

我的问题是,如何在不中断其他函数的情况下,重复地从API检索数据/重新运行最初获取数据的代码?

EN

回答 1

Stack Overflow用户

发布于 2019-03-24 14:17:17

下面是一个从Open获取ISS位置、经度和纬度的完整脚本,通知并将其打印在线程上。

我设置了1秒的时间。您可以启动和停止线程。现在试着添加你的函数。

请注意,您必须导入此模块

导入请求

导入json

代码语言:javascript
复制
import tkinter as tk
import threading
import queue
import datetime
import time
import requests
import json

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:
            
            response = requests.get("http://api.open-notify.org/iss-now.json")
            data = response.json()
            time.sleep(1)
            self.queue.put(data)
                                    

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 self.my_thread is not None:
            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)
                print("The ISS is currently over: {0} ".format(ret['iss_position']))
            except queue.Empty:
                pass                    
                   
    def on_close(self):
        if self.my_thread is not None:
            if(threading.active_count()!=1):
                self.my_thread.stop()

        self.master.destroy()
    
if __name__ == '__main__':
    app = App()
    app.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55319748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档