相关疑难解决方法(0)

如何在Python中每60秒异步执行一个函数?

我想在Python上每60秒执行一次函数,但我不希望同时被阻塞.

我该如何异步进行?

import threading
import time

def f():
    print("hello world")
    threading.Timer(3, f).start()

if __name__ == '__main__':
    f()    
    time.sleep(20)
Run Code Online (Sandbox Code Playgroud)

使用此代码,函数f在20秒time.time内每3秒执行一次.最后它给出了一个错误,我认为这是因为threading.timer还没有被取消.

我该如何取消?

提前致谢!

python asynchronous function call

58
推荐指数
2
解决办法
7万
查看次数

如何定期更改 tkinter 图像?

我有一个图像保存在一个文件中test.bmp,该文件每秒被覆盖 2 次
\n(我想每秒显示 2 个图像)。

\n\n

这是我到目前为止所拥有的:

\n\n
import tkinter as tk\nfrom PIL import Image, ImageTk\n\nroot = tk.Tk()\nimg_path = 'test.bmp'\nimg = ImageTk.PhotoImage(Image.open(img_path), Image.ANTIALIAS))\n\ncanvas = tk.Canvas(root, height=400, width=400)\ncanvas.create_image(200, 200, image=img)\ncanvas.pack()\n\nroot.mainloop()\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我不知道如何每隔 \xc2\xbd 秒刷新图像?
\n我正在使用 Python3 和 Tkinter。

\n

python image tkinter python-3.x

3
推荐指数
1
解决办法
3638
查看次数

使用来自第二个线程的数据更新 Tkinter-GUI 中的数据

问题是我的解决方案是否是一种使用来自另一个线程的数据更新 Tkinter-GUI 的保存和 pythonic 方式?是Lock必需的吗?或者怎么能在Queue这里提供帮助?此示例运行良好,但原始应用程序需要处理复杂得多的数据。

请专注于AsyncioThread.create_dummy_data()最小的工作示例。该示例有两个线程。一个运行Tkinter -mainloop,第二个线程运行asyncio -loop。异步循环模拟获取一些数据并tkinter.Label用这些数据刷新一些数据。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# restrict to Python3.5 or higher because of asyncio syntax

# based on </sf/answers/3354408991/>

from tkinter import *
import asyncio
import threading
import random


class AsyncioThread(threading.Thread):
    def __init__(self, asyncio_loop, theWindow):
        self.asyncio_loop = asyncio_loop
        self.theWindow = theWindow
        self.maxData = len(theWindow.varData)
        threading.Thread.__init__(self)


    def run(self):
        self.asyncio_loop.run_until_complete(self.do_data())


    async def do_data(self):
        """ Creating and starting 'maxData' asyncio-tasks. """ …
Run Code Online (Sandbox Code Playgroud)

python asynchronous tkinter python-multithreading python-asyncio

1
推荐指数
1
解决办法
6593
查看次数