2014-10-09 56 views
2

我有一个框架,一旦用户点击退出按钮,我想要一个对话框打开并询问他是否真的想关闭窗口。如何在wxPython中引发wx.EVT_CLOSE之后停止关闭窗口?

所以我做:

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实例?

+0

嗯,在我的代码中,似乎我甚至不需要它。如果我捕获该事件并且不显式调用'self.Destroy()',则该窗口不会关闭。我也不会调用'event.Skip()'。我或者自己关闭窗户。就是这样。 – Fenikso 2014-10-10 15:06:08

+0

你可以发布你的代码吗?不太明白你做了什么! – 2014-10-10 16:46:36

回答

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() 
+0

甜,谢谢!工作得更好! – 2014-10-10 17:58:05

+0

@ G-XP-MIA请注意[文档](http://www.wxpython.org/docs/api/wx.CloseEvent-class.html)建议您*如果不'请*调用'Veto'摧毁窗口:*“如果你没有销毁窗口,你应该调用'Veto'来让调用代码知道你没有销毁窗口,这允许'wx.Window.Close'函数返回True或者根据关闭指示是否兑现而错误。“* – dano 2014-10-10 22:42:56

+0

@dano有趣。我从来没有真正阅读关闭事件的文档。 – Fenikso 2014-10-11 20:30:07

2

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

+0

调用event.Veto(True)不起作用。刚刚测试过。当我编码并运行代码时,当我点击对话框上的“否”时,它会关闭。我这样做的方式工作,但提出了一个错误。但你错误地给了我答案,事件就是“事件”。所以我必须调用的是wx.CloseEvent.Veto(event,True) - 并且完美地工作。 – 2014-10-10 12:16:20

+0

这很奇怪,因为'event.Veto(True)'和'wx.CloseEvent(event,True)'是等价的。如果一个人工作,另一个人也应该工作。无论如何,很高兴你的工作... – dano 2014-10-10 12:31:52

相关问题