Index: events/activity.py =================================================================== --- events/activity.py (revision 2546) +++ events/activity.py (working copy) @@ -156,9 +156,7 @@ def call(self, func, *args, **kw): """Call `func(*args, **kw)` at the next opportunity""" self._call_queue.append((func, args, kw)) - if not self.initialized: - self._setup() - self.initialized = True + self._initialize() trellis.on_undo(self._call_queue.pop) self._callback_if_needed() @@ -174,16 +172,21 @@ f(*args, **kw) self._callback_if_needed() if Time.auto_update: - Time.tick() + Time.tick() else: Time.advance(self._next_time or 0) + def _initialize(self): + if not self.initialized: + self._setup() + self.initialized = True + decorators.decorate(trellis.modifier) def _callback_if_needed(self): if self._call_queue and not self._callback_active: self._arrange_callback(self._callback) self._callback_active = True - + decorators.decorate(trellis.modifier) def _split_queue(self, count): queue = self._call_queue @@ -293,15 +296,31 @@ unit test without mocking 'wx' (which I haven't tried to do. Use at your own risk. :( """ - context.replaces(EventLoop) + context.replaces(EventLoop) wx = None + def run(self): + """ + Start the wx main loop if necessary. + If called from within main loop it will still work, except for the `stop_requested` part. + """ + self._initialize() + if self.wx.GetApp().IsMainLoopRunning(): + self.call(Time.tick) + self.running = True + else: + super(WXEventLoop, self).run() + trellis.perform() def _ticker(self): if self.running: if Time.auto_update: if self._next_time is not None: - self.wx.FutureCall(self._next_time*1000, Time.tick) + msecs = int(self._next_time*1000) + if msecs > 0: + self.wx.CallLater(msecs, Time.tick) + else: + self.wx.CallAfter(Time.tick) if self.stop_requested: self.wx.GetApp().ExitMainLoop()