Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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脚本?_Python_User Interface_Wxpython_Refresh_Reload - Fatal编程技术网

如何每隔一段时间刷新一次Python脚本?

如何每隔一段时间刷新一次Python脚本?,python,user-interface,wxpython,refresh,reload,Python,User Interface,Wxpython,Refresh,Reload,所以我创建了一个类,其中包含使用wxPython的GUI。 如何使它每分钟刷新一次?我不使用wxPython,但是如果有一个名为refresh或类似的方法,您可以启动一个线程,每分钟调用该方法 from threading import Thread from time import sleep def refreshApp(app, timespan): while app.isRunning: app.refresh() sleep(timespan

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

我不使用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

对于间隔时间发生的事情,请使用。发件人:

由于某种原因,当我尝试时,这个代码不起作用。原因是计时器必须是类成员。如果将该代码放入init()方法并添加self。在定时器之前,它应该工作。如果没有,请尝试使_timer()也成为类成员。-巴勃朗托尼奥酒店


当计时器运行时,我在关闭帧时遇到问题

我是这样处理的:


正如Niklas所建议的,我认为您正在寻找Refresh()方法:

你是说我的编码风格吗?不需要添加额外的空格来对齐这样的东西,也不需要空格来执行
target=worker
。我确信这是@MattJ所指的对齐间距。此外,通常人们只在必要时才使用尾随逗号。他也可能不喜欢你使用非描述性的名字,比如
t
worker
或者使用
60。
如果
60
就可以了。我想,像我这样导入的意图更清楚。我同意这一点。我认为,做
target=worker
也更清楚。但我会修改代码,使其适合PEP8
Refresh
只需重新绘制部分窗口。仍然需要调用
Refresh
“偶尔刷新一下”…是的,你的帖子很好地涵盖了这一部分。Timer肯定是这里的方法。明白了,但不知何故,Refresh()方法不适用于wx.Frame。。。不知道为什么,它肯定每隔几秒钟循环一次(打印一些内容以确保),但Refresh()不起作用:\n如果在调用Refresh()之后立即添加对帧的Update()方法的调用,是否会发生任何变化?
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
def on_close(event):
    timer.Stop()
    frame.Destroy()

wx.EVT_CLOSE(frame, on_close)