在wxPython中引发wx.EVT_CLOSE后,如何阻止窗口关闭?

时间:2014-10-09 22:54:01

标签: python python-2.7 events wxpython

我有一个框架,一旦用户点击退出按钮,我想要一个对话框打开并询问他是否真的要关闭窗口。

所以我做了:

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

然后我有回调:

def OnCloseWindow(self, event):
    dialog = wx.MessageDialog(self, message = "Are you sure you want to quit?", caption = "Caption", style = wx.YES_NO, pos = wx.DefaultPosition)
    response = dialog.ShowModal()

    if (response == wx.ID_YES):
        Pairs = []
        self.list_ctrl_1.DeleteAllItems()
        self.index = 0
        self.Destroy()
    elif (response == wx.ID_NO):
        wx.CloseEvent.Veto(True)
    event.Skip()

这样可行,但是,我收到错误:

TypeError: unbound method Veto() must be called with CloseEvent instance as first argument (got bool instance instead)

如何捕获引发的事件的closeWindows实例?

2 个答案:

答案 0 :(得分:2)

您想拨打event.Veto(True),而不是wx.CloseEvent.Veto(True)eventwx.CloseEvent的一个实例 - 这就是您想要的Veto。现在你试图在Veto类本身上调用wx.CloseEvent,这是没有意义的。

答案 1 :(得分:2)

你真的不需要做那么多。如果您捕获到该事件但未调用event.Skip(),则不会向前传播。因此,如果您发现该事件并且未致电event.Skip()self.Destroy(),则该窗口将保持打开状态。

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.panel = wx.Panel(self)
        self.Bind(wx.EVT_CLOSE, self.on_close)
        self.Show()

    def on_close(self, event):
        dialog = wx.MessageDialog(self, "Are you sure you want to quit?", "Caption", wx.YES_NO)
        response = dialog.ShowModal()
        if response == wx.ID_YES:
            self.Destroy()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()