问题描述
我正在尝试用 wx python 编写一个 gui 应用程序,我需要控制计时器事件的间隔.这是我目前的代码:
I am trying to write a gui application with wx python and I need to control the interval of the timer event. Here is my code currently:
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(750) # start timer after a delay
这是正确的框架,但我无法控制间隔或 EVT_TIMER 发生的频率.我一直在尝试使用 wx TimerEvent 类,但没有任何运气.我觉得这应该是我需要的,但它不起作用:
This is the right framework but I cannot control the interval or how often the EVT_TIMER occurs. I have been trying to figure out using the wx TimerEvent class but without any luck. I feel like this should be what I need but it isn't working:
self.timer = wx.Timer(self)
self.timerEvent = wx.TimerEvent(self.timer.GetId(),10)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
谢谢!
推荐答案
我写了一个 定时器教程 可能会帮助您解决这个问题.基本上你按照你在第一个代码片段中提到的那样做.您必须启动计时器并向其传递一个以毫秒为单位的值.所以 1000 = 1 秒.wx.TimerEvent 不需要那个位.至少,我从来不需要那个.
I wrote a tutorial on timers a while back that might help you figure this out. Basically you do as you mentioned in the first code snippet. You have to Start the timer and pass it a value in milliseconds. So 1000 would = 1 second. You don't need that bit with the wx.TimerEvent. At least, I've never needed that.
无论如何,在您的示例中,计时器事件每 750 毫秒或不到一秒触发一次.我认为如果您机器的 CPU 被锁定,它可以中断或延迟计时器事件,但否则它们非常可靠.
Anyway, the timer event fires every 750 milliseconds in your example, or a little less than a second. I think if you machine's CPU gets pegged, it can interrupt or delay timer events, but otherwise they're very reliable.
这篇关于wxpython 定时器事件间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!