如何使用wxpython禁用OSX中的窗口关闭按钮?
问题描述:
我正在使用wxpython的osx应用程序。当用户单击窗口关闭按钮时,我想将窗口最小化为停靠栏,以便可以从停靠栏中恢复窗口。我怎样才能做到这一点?目前我有问题恢复窗口,因为当用户点击关闭按钮时会被破坏。我怎样才能防止呢?提前如何使用wxpython禁用OSX中的窗口关闭按钮?
答
你就不能绑定事件EVT_CLOSE
,并尽量减少使用,而不是你的处理器收盘为EVT_ICONIZE
...
def __init__(self):
...
self.Bind(wx.EVT_CLOSE, self.onCloseWindow)
...
def onCloseWindow(self, event):
... do something else instead of closing ...
...
答
方式
感谢我做到这一点是这样的:
在__init__
方法中,为wx.EVT_CLOSE
事件和一个菜单项设置了“真正”退出选项。你需要这个或者你永远不能关闭你的程序。
def OnClose(self,evt):
#Turn closes into hides unless this is a quit application message/or OS shutting down
if evt.CanVeto():
self.Hide()
evt.Veto()
else:
#if we don't veto it we allow the event to propogate
evt.Skip()
def OnMenuExit(self,evt):
#Event handler for exit menu item
self.Close(force=True) #Stops the close handler vetoing it
你也应该确保在__init__
你叫wx.App.SetMacExitMenuItemId([ID OF YOUR EXIT MENU ITEM HERE])
这样在您的船坞上下文菜单路线退出项目到您的正确的菜单句柄。
当窗口关闭时,这会为您提供一个很好的mac-ish hide。您需要意识到应用程序仍在运行,并且可以调用其菜单,因为菜单栏仍然在屏幕的顶部。在菜单事件处理程序中对self.Show()
的战略调用是您的朋友。
当你的应用程序窗口被隐藏时(例如点击dock来重新显示窗口),你也可以很好地利用wx.TaskBarIcon与dock进行良好的交互。