相当简单的问题,但我似乎无法找到答案。我有一个GUI,它有一个取消按钮,要求用户在按下它时中止所有未保存的更改。 GUI也有wx.CLOSE_BOX
,但这只是关闭它,因为它没有绑定到我的OnCancel
函数。我如何绑定它?
我尝试的事情:
self.Bind(wx.EVT_CLOSE, lambda event: self.OnCancel(event, newCRNum), wx.CLOSE_BOX)
#This gives an AssertionError, replacing wx.EVT_CLOSE with wx.EVT_BUTTON also
# gives the same error
self.Bind(wx.EVT_CLOSE, lambda event: self.OnCancel(event, newCRNum))
#This binds any time ```self.Close(True)``` occurs (which makes sense) but
# is not what I want. There are other buttons which close the GUI which should not
# use the OnCancel function
提前感谢您的帮助
编辑:以下代码有助于澄清我在寻找的内容
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
newCRNum = 0
cancelBtn = wx.Button(self, -1, "Cancel")
self.Bind(wx.EVT_BUTTON, lambda event: self.OnCancel(event, newCRNum), cancelBtn)
def OnCancel(self, event, CRNum):
dlg = wx.MessageDialog(self, "Are you sure you want to cancel? All work will be lost and CR number will not be reserved.", "Cancel CR", wx.YES_NO|wx.NO_DEFAULT|wx.ICON_EXCLAMATION)
if dlg.ShowModal() == wx.ID_YES:
self.Destroy()
else:
dlg.Destroy
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.MainLoop()
所以这样做会创建一个异想天开的大取消按钮。按下此按钮时,会弹出一个对话框,提示用户是否确实要退出。如果他们说是,整个gui关闭,如果没有,只关闭对话框。
当用户按下GUI右上角的红色(X)按钮时,我希望发生同样的事情。由于是一个按钮,我认为它可以绑定到我的OnCancel
按钮,但我该怎么做?
答案 0 :(得分:0)
AssertionError
的原因:第三个参数必须是source
小部件,如下所示。
self.Bind(wx.EVT_CLOSE, lambda event: self.OnCancel(event, newCRNum), self)
请尝试以下示例:
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
newCRNum = 0
self.Bind(wx.EVT_CLOSE, lambda event: self.OnCancel(event, newCRNum))
def OnCancel(self, event, num):
dlg = wx.MessageDialog(self, 'Do you want close?', 'Sure?',
wx.OK|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
if result == wx.ID_OK:
event.Skip()
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.MainLoop()