Custom OS X Dock Bar Icon

Introduction

This recipe shows how to create a custom icon for your applicationon the OS X Dock Bar. The code was blatantly ripped from the wxPython demo and pieced together as needed.

Code Sample

   1 #!/usr/bin/env pythonw
   2 
   3 import wx
   4 import wx.lib.embeddedimage
   5 
   6 WXPdemo = wx.lib.embeddedimage.PyEmbeddedImage(
   7     "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAWlJ"
   8     "REFUWIW1V1sSwjAIBMebeBU9db2KZ8EPmxbCI4TUnXGskWaXDQktwhjErjERP4XRhER08iPi"
   9     "5SKiyQR5JyI7xxB3j7wn5GI6V2hFxM0gJtjYANFBiIjQu7L/1lYlwR0QxLDZhE0II1+CtwRC"
  10     "RI8riBva7DL7CC9VAwDbbxwKtdDXwBi7K+1zCP99T1vDFedd8FBwYd6BCAUXuACEF7QsbET/"
  11     "FaHs+gDQw4vOLNHkMojAnTw8nlNipIiwmR0DCXJbjCXkFCAL23BnpQgRWt1EMbyujCK9AZzZ"
  12     "f+b3sX0oSqJQ6EorFeT4NiL6Wtj0+LXnQAzThYoAAsN6ehqR3sHExmcEqGeFApQLcTvm5Kt9"
  13     "wkHGgb+RZwSkyc1dwOcpCtCoNKSz6FRCUQ3o7Nn+5Y+Lg+y5CIXlcyAk99ziiQS32+svz/UY"
  14     "vClJoLpIC8gi+VwwfDecEiEtT/WZTJDf94uk1Ru8vbz0cvoF7S2DnpeVL9UAAAAASUVORK5C"
  15     "YII=")
  16 
  17 class DemoTaskBarIcon(wx.TaskBarIcon):
  18     TBMENU_RESTORE = wx.NewId()
  19     TBMENU_CLOSE   = wx.NewId()
  20     TBMENU_CHANGE  = wx.NewId()
  21     TBMENU_REMOVE  = wx.NewId()
  22     
  23     def __init__(self, frame):
  24         wx.TaskBarIcon.__init__(self)
  25         self.frame = frame
  26 
  27         # Set the image
  28         icon = self.MakeIcon(WXPdemo.GetImage())
  29         self.SetIcon(icon, "wxPython Demo")
  30         self.imgidx = 1
  31         
  32         # bind some events
  33         self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate)
  34         self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=self.TBMENU_RESTORE)
  35         self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)
  36 
  37 
  38     def CreatePopupMenu(self):
  39         """
  40         This method is called by the base class when it needs to popup
  41         the menu for the default EVT_RIGHT_DOWN event.  Just create
  42         the menu how you want it and return it from this function,
  43         the base class takes care of the rest.
  44         """
  45         menu = wx.Menu()
  46         menu.Append(self.TBMENU_RESTORE, "Restore wxPython Demo")
  47         menu.Append(self.TBMENU_CLOSE,   "Close wxPython Demo")
  48         return menu
  49 
  50 
  51     def MakeIcon(self, img):
  52         """
  53         The various platforms have different requirements for the
  54         icon size...
  55         """
  56         if "wxMSW" in wx.PlatformInfo:
  57             img = img.Scale(16, 16)
  58         elif "wxGTK" in wx.PlatformInfo:
  59             img = img.Scale(22, 22)
  60         # wxMac can be any size upto 128x128, so leave the source img alone....
  61         icon = wx.IconFromBitmap(img.ConvertToBitmap() )
  62         return icon
  63     
  64 
  65     def OnTaskBarActivate(self, evt):
  66         if self.frame.IsIconized():
  67             self.frame.Iconize(False)
  68         if not self.frame.IsShown():
  69             self.frame.Show(True)
  70         self.frame.Raise()
  71 
  72 
  73     def OnTaskBarClose(self, evt):
  74         wx.CallAfter(self.frame.Close)
  75 
  76 
  77 
  78 class MainFrame(wx.Frame):
  79     def __init__(self, parent):
  80         wx.Frame.__init__(self, parent, title="Hello World")
  81         self.tbicon = DemoTaskBarIcon(self)
  82         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  83 
  84     def OnCloseWindow(self, evt):
  85         self.tbicon.Destroy()
  86         evt.Skip()
  87         
  88 
  89 app = wx.App(redirect=False)
  90 frame = MainFrame(None)
  91 frame.Show(True)
  92 app.MainLoop()

Comments

The icon could be in a separate file, I just embedded it as I didn't want to accidentally lose where it was....

Custom Mac OsX Dock Bar Icon (last edited 2008-12-19 19:12:12 by c-67-168-196-237)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.