如何使用wxpython禁用OSX中的窗口关闭按钮?

时间:2021-12-23 07:11:51

i am working on an application for osx using wxpython. I want to minimize window to dockbar when user clicks on the window close button so that it can be restored from the dockbar. How can i do that? Currently i am having problem restoring window because it gets destroyed when user clicks on the close button. how can i prevent that?

我正在使用wxpython处理osx的应用程序。我想在用户点击窗口关闭按钮时最小化窗口到停靠栏,以便可以从停靠栏恢复它。我怎样才能做到这一点?目前我有恢复窗口的问题,因为它在用户点击关闭按钮时被破坏。我怎么能防止这种情况?

Thanks in advance

提前致谢

2 个解决方案

#1


The way I do it is like this:

我这样做的方式是这样的:

In the __init__ method set up handlers for the wx.EVT_CLOSE event and a menu item that is your "real" quit option. You need this or you can never close your program.

在__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

You should also make sure that in __init__ you call wx.App.SetMacExitMenuItemId( [ID OF YOUR EXIT MENU ITEM HERE] ) so that the quit item in your dock context menu routes to your correct menu handler.

您还应确保在__init__中调用wx.App.SetMacExitMenuItemId([此处为您的退出菜单项目的ID]),以便Dock上下文菜单中的退出项目路由到正确的菜单处理程序。

This gives you a nice mac-ish hide when the window is closed. You need to be aware that the app is still runnning and its menus can be called as the menu bar is still in the top of the screen. Strategic calls to self.Show() in menu event handlers are your friend here.

当窗口关闭时,这会给你一个很好的mac-ish hide。您需要知道应用程序仍在运行,并且可以调用其菜单,因为菜单栏仍位于屏幕顶部。对菜单事件处理程序中的self.Show()的战略调用是你的朋友。

You can also make good use of wx.TaskBarIcon to interact nicely with the dock when your app window is hidden (e.g. click on dock to reshow window).

当您的应用程序窗口被隐藏时,您还可以充分利用wx.TaskBarIcon与Dock进行良好的交互(例如,单击Dock以重新显示窗口)。

#2


Can't you just bind the event EVT_CLOSE and minimize instead of closing using your handler for EVT_ICONIZE

你不能只是绑定事件EVT_CLOSE并最小化而不是使用EVT_ICONIZE的处理程序关闭

...
def __init__(self):
  ...
  self.Bind(wx.EVT_CLOSE, self.onCloseWindow)
  ...
def onCloseWindow(self, event):
  ... do something else instead of closing ...
...

#1


The way I do it is like this:

我这样做的方式是这样的:

In the __init__ method set up handlers for the wx.EVT_CLOSE event and a menu item that is your "real" quit option. You need this or you can never close your program.

在__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

You should also make sure that in __init__ you call wx.App.SetMacExitMenuItemId( [ID OF YOUR EXIT MENU ITEM HERE] ) so that the quit item in your dock context menu routes to your correct menu handler.

您还应确保在__init__中调用wx.App.SetMacExitMenuItemId([此处为您的退出菜单项目的ID]),以便Dock上下文菜单中的退出项目路由到正确的菜单处理程序。

This gives you a nice mac-ish hide when the window is closed. You need to be aware that the app is still runnning and its menus can be called as the menu bar is still in the top of the screen. Strategic calls to self.Show() in menu event handlers are your friend here.

当窗口关闭时,这会给你一个很好的mac-ish hide。您需要知道应用程序仍在运行,并且可以调用其菜单,因为菜单栏仍位于屏幕顶部。对菜单事件处理程序中的self.Show()的战略调用是你的朋友。

You can also make good use of wx.TaskBarIcon to interact nicely with the dock when your app window is hidden (e.g. click on dock to reshow window).

当您的应用程序窗口被隐藏时,您还可以充分利用wx.TaskBarIcon与Dock进行良好的交互(例如,单击Dock以重新显示窗口)。

#2


Can't you just bind the event EVT_CLOSE and minimize instead of closing using your handler for EVT_ICONIZE

你不能只是绑定事件EVT_CLOSE并最小化而不是使用EVT_ICONIZE的处理程序关闭

...
def __init__(self):
  ...
  self.Bind(wx.EVT_CLOSE, self.onCloseWindow)
  ...
def onCloseWindow(self, event):
  ... do something else instead of closing ...
...