2009-04-14 64 views

回答

0

您的意思是你想要派遣活動?

:: wxPostEvent空隙 wxPostEvent(wxEvtHandler * DEST, wxEvent &事件)

在GUI應用程序,該功能 帖事件使用 wxEvtHandler :: AddPendingEvent指定DEST 對象。 否則,它立即使用 wxEvtHandler :: ProcessEvent調度事件 。有關詳細信息,請參閱 相關文檔 (和注意事項)。

包含文件

<wx/app.h>

wxPython API docs

+2

我不知道C,這是令人困惑的。你能給我我應該運行的Python系列嗎? – 2009-04-14 14:38:54

48

老話題,但我想我已經弄清了很長一段時間了,所以如果有其他人通過這裏尋找答案,這可能會有所幫助。

手動方式發佈事件,您可以使用

self.GetEventHandler().ProcessEvent(event) 

(wxWidgets的文檔here,wxPython的文檔here

wx.PostEvent(self.GetEventHandler(), event) 

wxWidgets docswxPython docs

其中event是您要發佈的活動。用例如事件構建事件

wx.PyCommandEvent(wx.EVT_BUTTON.typeId, self.GetId()) 

如果您想發佈EVT_BUTTON事件。使其成爲PyCommandEvent意味着它會向上傳播;其他事件類型默認情況下不會傳播。

您還可以創建自定義事件,以便隨身攜帶任何您想要的數據。這裏有一個例子:!

myEVT_CUSTOM = wx.NewEventType() 
EVT_CUSTOM = wx.PyEventBinder(myEVT_CUSTOM, 1) 

class MyEvent(wx.PyCommandEvent): 
    def __init__(self, evtType, id): 
     wx.PyCommandEvent.__init__(self, evtType, id) 
     myVal = None 

    def SetMyVal(self, val): 
     self.myVal = val 

    def GetMyVal(self): 
     return self.myVal 

(我想我發現在某處的郵件列表歸檔這個代碼,但我似乎無法再找到它,如果這是你的榜樣,謝謝請添加評論和採取信用!)

所以,現在,上傳自定義事件:

event = MyEvent(myEVT_CUSTOM, self.GetId()) 
event.SetMyVal('here is some custom data') 
self.GetEventHandler().ProcessEvent(event) 

,你可以綁定它就像任何其他事件

self.Bind(EVT_CUSTOM, self.on_event) 

,並在事件處理程序獲取自定義數據

def on_event(self, e): 
    data = e.GetMyVal() 
    print 'custom data is: {0}'.format(data) 

或者在事件構造函數中包含自定義數據並保存一個步驟:

class MyEvent(wx.PyCommandEvent): 
    def __init__(self, evtType, id, val = None): 
     wx.PyCommandEvent.__init__(self, evtType, id) 
     self.myVal = val 

希望這是有幫助的人。

5

有一個簡單,直接的方式與wxPython中的最新版本(見http://wiki.wxpython.org/CustomEventClasses)做到這一點:

# create event class 
    import wx.lib.newevent 
    SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent() 

    # post it, with arbitrary data attached 
    wx.PostEvent(target, SomeNewEvent(attr1=foo, attr2=bar)) 

    # bind it as usual 
    target.Bind(EVT_SOME_NEW_EVENT, target.handler)