AddPendingEvent无效

时间:2016-05-07 13:09:24

标签: python python-3.x wxpython

我想使用AddPendingEvent发送事件。但是,在调用AddPendingEvent后没有任何反应。以下是一个示例,其中按钮应该向帧发送wx.CloseEvent

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        super(wx.Frame, self).__init__(None, wx.ID_ANY, 'Test')

        self.button = wx.Button(self, wx.ID_ANY, 'Close', self.GetClientSize()/2)
        self.button.Bind(wx.EVT_BUTTON, self.OnButton)

        self.Bind(wx.EVT_CLOSE, self.OnClose)

        self.Show()

    def OnButton(self, event: wx.CommandEvent):
        self.AddPendingEvent(wx.CloseEvent())

    def OnClose(self, event: wx.CloseEvent):
        self.Destroy()

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
    app.MainLoop()

我还尝试了QueueEventwx.PostEvent,结果是一样的。

1 个答案:

答案 0 :(得分:0)

你应该去PyCommandEvent并创建&将事件类型为wx.EVT_CLOSE排队,如下所示:

self.AddPendingEvent(wx.PyCommandEvent(wx.EVT_CLOSE.typeId, self.GetId()))