如何在wxPython中提高wx.EVT_CLOSE后防止窗口关闭?

2024-09-30 06:19:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

所以我做了:

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

然后我回电话:

^{pr2}$

但是,我得到了一个错误:

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

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


Tags: instance用户self框架closebind错误按钮
2条回答

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

你不需要做那么多。如果捕获事件并且不调用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()

相关问题 更多 >

    热门问题