Have been wondering about this for days now:
I have a basic wxpython program like this:
from MyModule import *
class Form(wx.Panel):
def __init__(self, parent, id):
self.gauge = wx.Gauge(...)
...
def ButtonClick(self, event):
proc = LongProcess()
while (LongProcess):
self.gauge.SetValue(LongProcess.status)
wx.Yield()
which imports the MyModule.py:
from threading import *
class LongProcess(self):
def __init__(self):
Thread.__init__(self)
self.start()
def run(self):
for i in range(100):
Do_something()
self.status = i
This updates the gauge according to the value LongProcess.status, as expected. But the while-loop doesn’t seem appropriate as the whole program uses 100% cpu load because it continuously checks the status (not surprising, tho). Is there any way to send the status back to the “mother program” without doing that millions of times per second?
You can instantiate custom events from the non-GUI thread and wx.PostEvent them back to the GUI-thread. This is a thread-safe action. My use cases typically work like this:
Then I bind the custom event to update a dialog or textctrl/log or whatever. It’s surprisingly easy to do. If you’d like I can post some sample code of a little test case I wrote a while back when I was figuring this stuff out.
–Edit:
Okay here’s some code, first the threading example:
And a second, more simple example with custom events: