Wxpython wx.CallLater很晚了

Wxpython wx.CallLater很晚了,wxpython,Wxpython,在我的wxPython应用程序中,我有一个EVT\u IDLE处理程序,它调用一些必须每隔150毫秒左右调用一次的函数。调用函数后,处理程序调用: wx.CallLater(150,self._clear_idle_block_and_do) 该\u clear\u idle\u block\u和

在我的wxPython应用程序中,我有一个
EVT\u IDLE
处理程序,它调用一些必须每隔150毫秒左右调用一次的函数。调用函数后,处理程序调用:

wx.CallLater(150,self._clear_idle_block_and_do)
\u clear\u idle\u block\u和
功能基本上会发布另一个
EVT\u idle
事件,继续循环

现在我注意到,当GUI中的其他小部件正在努力工作时,
EVT\u IDLE
事件处理程序几乎不会被调用!有时它需要4秒钟才能调用,这太多了


这是因为wx.CallLater的性能不好吗?我能做些什么吗?

等待时间太长,因为它等待空闲时间。显然,如果你有“其他小部件正在努力工作”,那么很快就不会有空闲时间了。如果需要周期性事件,请使用计时器。

等待时间太长,因为它等待空闲时间。显然,如果你有“其他小部件正在努力工作”,那么很快就不会有空闲时间了。如果您想要周期性事件,请使用计时器。

这里有一个模块,可以创建一个不会出现此问题的计时器

#threadtimer.py

import threading
import time
import wx

wxEVT_THREAD_TIMER = wx.NewEventType()
EVT_THREAD_TIMER = wx.PyEventBinder(wxEVT_THREAD_TIMER, 1)

class ThreadTimer(object):
   def __init__(self, parent):
        self.parent = parent
        self.thread = Thread()
        self.thread.parent = self
        self.alive = False

   def start(self, interval):
       self.interval = interval
       self.alive = True
       self.thread.start()

   def stop(self):
       self.alive = False

class Thread(threading.Thread):
    def run(self):
       while self.parent.alive:
           time.sleep(self.parent.interval / 1000.0)
           event = wx.PyEvent()
           event.SetEventType(wxEVT_THREAD_TIMER)
           wx.PostEvent(self.parent.parent, event)
要在程序中使用:

import threadtimer

timer = threadtimer.ThreadTimer(window)
timer.start(150)
window.Bind(threadtimer.EVT_THREAD_TIMER, function_to_call)

这里有一个模块,它创建了一个没有这个问题的计时器

#threadtimer.py

import threading
import time
import wx

wxEVT_THREAD_TIMER = wx.NewEventType()
EVT_THREAD_TIMER = wx.PyEventBinder(wxEVT_THREAD_TIMER, 1)

class ThreadTimer(object):
   def __init__(self, parent):
        self.parent = parent
        self.thread = Thread()
        self.thread.parent = self
        self.alive = False

   def start(self, interval):
       self.interval = interval
       self.alive = True
       self.thread.start()

   def stop(self):
       self.alive = False

class Thread(threading.Thread):
    def run(self):
       while self.parent.alive:
           time.sleep(self.parent.interval / 1000.0)
           event = wx.PyEvent()
           event.SetEventType(wxEVT_THREAD_TIMER)
           wx.PostEvent(self.parent.parent, event)
要在程序中使用:

import threadtimer

timer = threadtimer.ThreadTimer(window)
timer.start(150)
window.Bind(threadtimer.EVT_THREAD_TIMER, function_to_call)