如何定期刷新Python脚本?
我创建了一个类,这个类使用wxPython来做图形界面(GUI)。我想知道怎么让它每分钟自动刷新一次?
3 个回答
0
正如Niklas所建议的,我觉得你在找的是Refresh()这个方法:http://wxpython.org/docs/api/wx.Window-class.html#Refresh。
0
我不太使用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规范
2
对于需要定时执行的事情,可以使用一个叫做 Timer 的东西。来自 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() 也变成类的成员。-- PabloAntonio
当我有一个定时器在运行时,关闭窗口会遇到问题。
这是我解决这个问题的方法:
def on_close(event):
timer.Stop()
frame.Destroy()
wx.EVT_CLOSE(frame, on_close)