我如何检测关闭 wxPython?
How do I detect closing wxPython?
我正在使用 wxpython...如果有人单击右上角的红色 'X'(关闭按钮),我如何检测并执行功能?代码是什么?有人可以帮帮我吗?谢谢!
您正在寻找 EVT_CLOSE
.
例如
import wx
class Test(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Main Window",size = (300,200))
panel = wx.Panel(self)
menubar=wx.MenuBar()
firstm=wx.Menu()
fm1 = wx.MenuItem(firstm, -1, 'Quit\tAlt+Q')
firstm.Append(fm1)
self.Bind(wx.EVT_MENU, self.OnExit, id=fm1.GetId())
# Catch Clicking on the Corner X to close
self.Bind(wx.EVT_CLOSE, self.OnExit)
menubar.Append(firstm,"File")
self.SetMenuBar(menubar)
t = wx.StaticText(panel,-1,"Testing 1 2 3 ....", pos=(10,20))
def OnExit(self, event):
# To discover how you got here,
# you can either test the event type or define a separate function for EVT_CLOSE,
# most of the time you don't care
if event.GetEventType() == wx.EVT_CLOSE.typeId:
print("Close using X")
else:
print("Close using Menu or Alt+Q")
self.Destroy()
if __name__=='__main__':
app=wx.App()
frame=Test(None)
frame.Show()
app.MainLoop()
我正在使用 wxpython...如果有人单击右上角的红色 'X'(关闭按钮),我如何检测并执行功能?代码是什么?有人可以帮帮我吗?谢谢!
您正在寻找 EVT_CLOSE
.
例如
import wx
class Test(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Main Window",size = (300,200))
panel = wx.Panel(self)
menubar=wx.MenuBar()
firstm=wx.Menu()
fm1 = wx.MenuItem(firstm, -1, 'Quit\tAlt+Q')
firstm.Append(fm1)
self.Bind(wx.EVT_MENU, self.OnExit, id=fm1.GetId())
# Catch Clicking on the Corner X to close
self.Bind(wx.EVT_CLOSE, self.OnExit)
menubar.Append(firstm,"File")
self.SetMenuBar(menubar)
t = wx.StaticText(panel,-1,"Testing 1 2 3 ....", pos=(10,20))
def OnExit(self, event):
# To discover how you got here,
# you can either test the event type or define a separate function for EVT_CLOSE,
# most of the time you don't care
if event.GetEventType() == wx.EVT_CLOSE.typeId:
print("Close using X")
else:
print("Close using Menu or Alt+Q")
self.Destroy()
if __name__=='__main__':
app=wx.App()
frame=Test(None)
frame.Show()
app.MainLoop()