Updating GUI from sub-threads

Frank Aune Frank.Aune at broadpark.no
Fri Apr 27 02:10:30 PDT 2007


Hello,

Ive got a threaded application, where sub-threads sometimes needs to update 
the GUI. Im using the following mechanism to accomplish this in a thread-safe 
way.

---
__EVT_THREAD__     = wx.NewEventType()

class ThreadEvent(wx.PyEvent):
    def __init__(self, func, *args, **kwargs):
        wx.PyEvent.__init__(self)
        self.SetEventType(__EVT_THREAD__)
        self.__func = func
        self.__args = args
        self.__kwargs = kwargs

    def execute(self):
        self.__func(*self.__args, **self.__kwargs)


def popMessageDialog(self, cap, msg, style=wx.OK|wx.ICON_ERROR|wx.CENTRE):
        """
        Method for popping message dialog from thread
        """
        dlg = wx.MessageDialog(self.frame,
                               message='%s' % msg,
                               caption='%s' % cap,
                               style=style)

        wx.PostEvent(self.frame, ThreadEvent(dlg.ShowModal))

---

This is an excerpt, but you get the idea. Im calling popMessageDialog from my 
threads.

This almost always works, however sometimes the dialog will come up with no 
widgets inside and the GUI will then not be updated anymore. You can infact 
still press the OK button if you know where its located in the message 
dialog, but no GUI updates are performed after this - not even in the main 
window (Tested on GNU/Linux both wx-2.6 and wx-2.8, and no difference).

Ive got lots of other GUI update methods called from threads too in a similar 
way as popMessageDialog, and they all work as expected and never cause any 
similar problems. 

Any input greatly appreciated.

Best regards,
Frank Aune




More information about the wxpython-users mailing list