tags:

views:

498

answers:

2

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?

Thanks in advance

A: 

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

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

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.

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.

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.

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).

Gareth Simpson