AddPendingEvent 无效

AddPendingEvent has no effect

我想使用 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,结果都是一样的。

您应该按照 PyCommandEvent 创建一个 wx.EVT_CLOSE 类型的事件并将其加入队列,如下所示:

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