Creating custom event in WxPython
fraca7
fraca7 at free.fr
Thu Sep 1 10:25:05 EDT 2005
NutJob at gmx.net a écrit :
> Now when my socket thread detects an incoming message, I need my main
> thread to interpret the message and react to it by updating the GUI.
> IMO the best way to achieve this is by having my socket thread send a
> custom event to my application's event loop for the main thread to
> process. However, I'm at a total loss as far as creating custom events
> is concerned. The WxWindows documentation isn't very helpful on this
> either.
I think this is what you're looking for:
# begin code
import wx
wxEVT_INVOKE = wx.NewEventType()
class InvokeEvent(wx.PyEvent):
def __init__(self, func, args, kwargs):
wx.PyEvent.__init__(self)
self.SetEventType(wxEVT_INVOKE)
self.__func = func
self.__args = args
self.__kwargs = kwargs
def invoke(self):
self.__func(*self.__args, **self.__kwargs)
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.Connect(-1, -1, wxEVT_INVOKE, self.onInvoke)
def onInvoke(self, evt):
evt.invoke()
def invokeLater(self, func, *args, **kwargs):
self.GetEventHandler().AddPendingEvent(InvokeEvent(func, args,
kwargs))
# end code
This way, if frm is an instance of MyFrame, invoking
frm.invokeLater(somecallable, arguments...) will invoke 'somecallable'
with the specified arguments in the main GUI thread.
I found this idiom somewhere on the Web and can't remember where.
HTH
More information about the Python-list
mailing list