python - 如何检测关闭 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()
推荐阅读
- javascript - 将其值移动到另一个数组后显示整个数组
- java - 无法读取 JSON:无法构造 `java.time.ZonedDateTime` 的实例(没有创建者,如默认构造,存在)
- sql - 如何从重复项中选择第一条记录,只有重复的列值
- javascript - ref 和全局变量有什么区别?
- sql - SQL - 增量案例语句 - 队列分析
- android - Android线性布局使高度适应元素
- angular - 如何在 Angular Ag Grid 中测试 AgRendererComponent 组件
- php - 在 PHP 中解析多维关联数组
- ios - 使用 TabView 时导致“AttributeGraph 前置条件失败”的原因是什么?
- winforms - Graphics.DrawString() 方法在字符串长度过长时抛出 GDI+ 异常