WxPython: using EVT_IDLE

I have defined a handler for EVT_IDLEwhich performs a specific background job for me. (This task is to complete the completed work of several processes and integrate it into any object, making visible changes in the graphical interface.)

The problem is that when the user does not move the mouse or does nothing, EVT_IDLEit is not called more than once. I would like this handler to work all the time. So I tried calling event.RequestMore()at the end of the handler. It works, but now it takes a lot of CPU. (I guess he just goes over this task too much.)

I am ready to limit the number of times a task will be executed per second; How to do it?

Or do you have another solution?

+3
source share
2 answers

Something like this (runs no more than every second):

...

def On_Idle(self, event):
    if not self.queued_batch:
        wx.CallLater(1000, self.Do_Batch)
        self.queued_batch = True

def Do_Batch(self):
    # <- insert your stuff here
    self.queued_batch = False

...

Oh, and don't forget to set self.queued_batch to False in the constructor and maybe call event.RequestMore () somehow in On_Idle.

+2
source

This sounds like an example of using wxTimerEvent instead of wxIdleEvent. When there is processing to call wxTimerEvent.Start (). If you have nothing to do, call wxTimerEvent.Stop () and call the processing methods from EVT_TIMER.

(note: I use from wxWidghets for C ++ and am not familiar with wxPython, but I assume they have a similar API)

0
source

Source: https://habr.com/ru/post/1706987/


All Articles