如何手动触发wx.Frame关闭事件(wxPython)?

时间:2017-04-05 19:24:20

标签: wxpython

我想登录错误并退出wxPython脚本,为此我需要触发put事件ID:wx.EVT_BUTTON.typeId

我有OnExit定义:

wx.ID_CLOSE

如何从我的代码生成和传播wx.ID_CLOSE事件?

我试过但它不起作用:

b_exit = wx.Button(self.statusBar, wx.ID_CLOSE, "Exit") b_exit.Bind(wx.EVT_BUTTON, self.OnExit) ... def OnExit(self, evt) self.Destroy()

2 个答案:

答案 0 :(得分:0)

OnExit更改为

def OnExit(self, evt)
   self.Close()

将触发EVT_CLOSE事件。确保从绑定关闭方法中调用event.Skip()。框架关闭后,Destroy会自动调用wx.Frame

附注:要手动触发事件,请参阅wx.PostEvent

答案 1 :(得分:0)

无法查看您的代码,很难知道 希望这个定义您自己的事件的小例子将允许您解决它。我不得不承认,我花了一段时间才开始了解它。

import wx
import wx.lib.newevent
NewEvent, EVT_MY_EVENT = wx.lib.newevent.NewEvent()
CMD_ID = wx.NewId()
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame()
        return True

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None, title="Window", pos = (100,150), size =(250,200))
        sizer = wx.BoxSizer()
        self.button1 = wx.Button(self, CMD_ID, label="Button 1")
        sizer.Add(self.button1)
        self.Bind(wx.EVT_BUTTON, self.OnButton,id=CMD_ID)
        self.Bind(EVT_MY_EVENT, self.OnMyEvent)
        self.Layout()
        self.Show()

    def OnButton(self, event):
        id = event.GetId()
        event = NewEvent(action="perform a defined action",button=id,other_setting=1234)
        wx.PostEvent(self, event)

    def OnMyEvent(self, event):
        button = event.button
        action = event.action
        other = event.other_setting
        print "event button number", button
        print "event action request", action
        print "event other", other

if __name__ == "__main__":
    app = MyApp()
    app.MainLoop()