python - wxpython frame initialization error with threads -
here's example:
class demoframe(wx.frame): def __init__(self, parent): wx.frame.__init__(self, parent) self.panel = wx.panel(self, -1) ... initialize other elements ... self.dostuff() def dostuff(self): self.panel.setbackgroundcolour(wx.colour(240, 240, 240)) ... ... now know not example of initializing gui since do something freeze gui while it's running, tweaked this:
import threading class demoframe(wx.frame): def __init__(self, parent): wx.frame.__init__(self, parent) self.panel = wx.panel(self, -1) ... initialize other elements ... dostuffthead = threading.thread(target = self.dostuff, ()) dostuffthead.start() def dostuff(self): wx.callafter(self.changebg, ) ... ... def changebg(self): self.panel.setbackgroundcolour(wx.colour(240, 240, 240)) above code should work same first 1 when do something blank, surprise noticed there's little background drawing glitches when running latter codes.
what part went wrong? isn't right way update gui in threads?
it's bad approach update gui worker thread, not event saying it's not thread safe. have communicate main thread update gui.
the best way achieve desired result user wx.postevent method. can create custom events needs, inheriting wx.pyevent, , better inherit threading.thread keep window want communicate within thread class instance variable.
the best illustration of how update gui having long-running task can found in wxpython wiki (first example).
Comments
Post a Comment