if wx.TaskBarIcon is created after a Frame on Mac, the exit event comes after the second click

Hello everyone,

I created a small test app using a wx.TaskBarIcon and I get different effects on mac, linux, and windows.
On linux and windows, I get the same thing, it works as expected (the quit menu prints to stdout), but on mac, if
I create the frame before the task bar icon, then the quit button only prints to stdout the second time it is chosen
(and also from then on), but not the first. If I inverse the frame and task bar icon creation, it works like on linux and
windows. Does anyone know why this is happening? Is there a workaround or something to do (other than creating
the task bar icon first)? I put the test code below.

Thank you,
Gabriel

···

#====================================================
import wx

class MyTaskBarIcon(wx.TaskBarIcon):
       def __init__(self, *args, **kwargs):
        wx.TaskBarIcon.__init__(self, *args, **kwargs)
        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
        icon = wx.Icon("logo.png", wx.BITMAP_TYPE_PNG)
        self.SetIcon(icon)
       def CreatePopupMenu(self):
        self.menu = wx.Menu()
        if(wx.Platform == "__WXMAC__"):
            wx.App.SetMacExitMenuItemId(wx.ID_EXIT)
        else:
            self.exitMenuItem = self.menu.Append(wx.ID_EXIT, "Exit")
        return self.menu
       def OnExit(self, evt):
        print "OnExit called"

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title="Hello World")

class MyApp(wx.App):
    def OnInit(self):
        frame = MainFrame() # Inverse these two lines, if the frame
        self.tbicon = MyTaskBarIcon() # is created first, you have to click on
                                                           # quit twice for it to call OnExit(), if
                                                           # the task bar icon is created first, then
                                                           # it works as expected, you only have to
                                                           # click once for it to be called.
               return True

if(__name__ == "__main__"):
    a = MyApp()
    a.MainLoop()
#====================================================

My guess is that since the default Quit item on the dock icon's menu is the same as the Quit item on the application menu then the event is going first to the frame and being handled there. It's not until the frame is gone that the menu event will be able to find its way to the taskbar icon. To test this theory try showing the frame and see how it behaves in each case.

···

On 10/1/09 5:24 AM, Gabriel Rossetti wrote:

Hello everyone,

I created a small test app using a wx.TaskBarIcon and I get different
effects on mac, linux, and windows.
On linux and windows, I get the same thing, it works as expected (the
quit menu prints to stdout), but on mac, if
I create the frame before the task bar icon, then the quit button only
prints to stdout the second time it is chosen
(and also from then on), but not the first. If I inverse the frame and
task bar icon creation, it works like on linux and
windows. Does anyone know why this is happening? Is there a workaround
or something to do (other than creating
the task bar icon first)? I put the test code below.

--
Robin Dunn
Software Craftsman