We are using TaskBarIcon WxPython 2.9 on Mac OSX 10.8.5 . We currently have a requirement to capture all Left, Right, and Double Click events when a user clicks on our application's TaskBarIcon. The problem we are facing is that these events do not fire. Any help in this regard will be very noticeable.
This is the code we use.
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()
NB We do not want to use CreatePopupMenu (because it only starts when left-clicking), instead we want to capture all three of the specified mouse events.
python wxpython macos
Ammar hasan
source share