如何偶尔刷新一次Python脚本?

发布于 2024-11-28 01:22:44 字数 54 浏览 0 评论 0原文

所以我创建了一个类,其中包含使用 wxPython 的 GUI。 你如何让它每分钟刷新一次?

So I've created a class, which contains GUI using wxPython.
How do you make it so that it refreshes itself say every minute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

注定孤独终老 2024-12-05 01:22:44

对于间隔发生的事情,请使用计时器。来自 WxPyWiki

def on_timer(event):
    pass  # do whatever

TIMER_ID = 100  # pick a number
timer = wx.Timer(panel, TIMER_ID)  # message will be sent to the panel
timer.Start(100)  # x100 milliseconds
wx.EVT_TIMER(panel, TIMER_ID, on_timer)  # call the on_timer function

出于某种原因,当我尝试此代码时,它不起作用。原因是计时器必须是班级成员。如果您将该代码放入 init() 方法中并添加 self.在计时器之前,它应该可以工作。如果没有,请尝试将 on_timer() 也设为类成员。 ——巴勃罗·安东尼奥


当计时器正在运行时,我在关闭框架时遇到问题。

这是我的处理方法:

def on_close(event):
    timer.Stop()
    frame.Destroy()

wx.EVT_CLOSE(frame, on_close)

For things that happen on intervals, use a Timer. From WxPyWiki:

def on_timer(event):
    pass  # do whatever

TIMER_ID = 100  # pick a number
timer = wx.Timer(panel, TIMER_ID)  # message will be sent to the panel
timer.Start(100)  # x100 milliseconds
wx.EVT_TIMER(panel, TIMER_ID, on_timer)  # call the on_timer function

For some reason, this code didn't work when I tried it. The reason was the timer had to be a class member. If you put that code into the init() method and add self. before timer, it should work. If it doesn't, try making on_timer() a class member too. -- PabloAntonio


I've had problems closing my frame, when there was a Timer running.

Here's how I handled it:

def on_close(event):
    timer.Stop()
    frame.Destroy()

wx.EVT_CLOSE(frame, on_close)
无人问我粥可暖 2024-12-05 01:22:44

我不使用 wxPython,但如果有一个名为 refresh 或类似方法的方法,您可以每分钟启动一个调用该方法的线程。

from threading import Thread
from time import sleep

def refreshApp(app, timespan):
    while app.isRunning:
        app.refresh()
        sleep(timespan)

refresher = Thread(target=worker, args=(myAppInstance, 60))
refresher.start()

编辑:修复代码,使其适合 PEP8

I don't work with wxPython, but if there is a method called refresh or something alike, you could start a Thread calling that method every minute.

from threading import Thread
from time import sleep

def refreshApp(app, timespan):
    while app.isRunning:
        app.refresh()
        sleep(timespan)

refresher = Thread(target=worker, args=(myAppInstance, 60))
refresher.start()

EDIT: fixed code so it fits into PEP8

恋你朝朝暮暮 2024-12-05 01:22:44

正如 Niklas 所建议的,我认为您正在寻找 Refresh() 方法: http ://wxpython.org/docs/api/wx.Window-class.html#Refresh

As Niklas suggested I think you're looking for the Refresh() method: http://wxpython.org/docs/api/wx.Window-class.html#Refresh .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文