我有一个框架,一旦用户单击退出按钮,我想打开一个对话框,问他是否真的要关闭窗口。
所以我做了:
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实例?
您实际上不需要做那么多。如果您捕获该事件并且不调用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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句