Mouse Events of WxPython TaskBarIcon on Mac OSX are not triggering

Viewed 966

We are using TaskBarIcon of WxPython 2.9 on Mac OSX 10.8.5, currently we have a requirement to grab all Left, Right and Double Click Events when user clicks on TaskBarIcon of our Application. The problem we are having is that these events are not being triggered. Any help in this regard would be much appreciable.

This is the code we are using

import os
import sys
import wx

__author__ = 'Ammar Hasan'

CURRENT_ABSOLUTE_PATH = os.path.dirname(sys.argv[0])
TRAY_ICON = CURRENT_ABSOLUTE_PATH + "/resources/task_icon.ico"
TRAY_ICON_TOOLTIP = "UI Application"


ID_SHOW_OPTION = wx.NewId()
ID_EDIT_OPTION = wx.NewId()
ID_EXIT_OPTION = wx.ID_EXIT


class Icon(wx.TaskBarIcon):

    def __init__(self, parent, icon, tooltip):
        super(Icon, self).__init__(iconType=wx.TBI_CUSTOM_STATUSITEM)
        self.set_icon(icon, tooltip)
        self.parent = parent
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_icon_click)
        self.Bind(wx.EVT_TASKBAR_RIGHT_DOWN, self.on_icon_click)
        self.Bind(wx.EVT_MENU, self.menu_item_click)

    def get_menu(self):
        menu = wx.Menu()
        menu.Append(ID_SHOW_OPTION, "&Show Option 1")
        menu.Append(ID_EDIT_OPTION, "&Edit Option 2")
        menu.AppendSeparator()
        menu.Append(ID_EXIT_OPTION, "E&xit")
        return menu

    def on_icon_click(self, event):
        if event:
            print "Event Triggered."
        menu = self.get_menu()
        self.PopupMenu(menu)

    def menu_item_click(self, event):
        if event.Id == ID_SHOW_OPTION:
            pass
        elif event.Id == ID_EDIT_OPTION:
            pass
        else:
            self.parent.quit()

    def make_icon(self, img):
        """
        The various platforms have different requirements for the
        icon size...
        """
        if "wxMSW" in wx.PlatformInfo:
            img = img.Scale(16, 16)
        elif "wxGTK" in wx.PlatformInfo:
            img = img.Scale(22, 22)
        # wxMac can be any size upto 128x128, so leave the source img alone....
        icon = wx.IconFromBitmap(img.ConvertToBitmap())
        return icon

    def set_icon(self, path, tool_tip):
        image = wx.Image(path)
        icon = self.make_icon(image)
        self.SetIcon(icon, tool_tip)

    # def CreatePopupMenu(self, event=None):
    #     self.on_icon_click(event)


class Frame(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(Frame, self).__init__(*args, **kwargs)
        self.app = wx.GetApp()
        self.icon = Icon(self, TRAY_ICON, TRAY_ICON_TOOLTIP)

    def quit(self):
        self.app.ExitMainLoop()

if __name__ == "__main__":
    app = wx.App()
    frame = Frame(None)
    frame.Show(False)
    app.SetTopWindow(frame)
    app.MainLoop()

N.B. We do not want to use CreatePopupMenu (because it is only triggered on Left Click), instead we want to grab all three specified mouse events.

1 Answers
Related