wxPython System Tray Icon
I'm trying to implement a icon in the system tray for my application however I have two problems.
Firstly, although the icon I am using is a .png with a transparent background the icon has an ugly white background.
Second, the Icon has a right click menu with the options "Show" and "Close" however for unknown reasons both say "Ctrl - Q" next to them. Not only did I not specify this, but the hotkey combo does nothing.
Here is the code I am using. It's almost directly lifted from the documentation:
class SysTray(wx.TaskBarIcon):
def __init__(self, parent, icon, text):
wx.TaskBarIcon.__init__(self)
开发者_高级运维self.parentApp = parent
self.SetIcon(icon, text)
self.CreateMenu()
def CreateMenu(self):
self.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.ShowMenu)
self.menu=wx.Menu()
self.menu.Append(wx.ID_OPEN, "Show")
self.menu.Append(wx.ID_EXIT, "Close")
def ShowMenu(self,event):
self.PopupMenu(self.menu)
Which is implemented using:
self.trayicon = SysTray(self, wx.Icon(TRAY_ICON, wx.BITMAP_TYPE_PNG), TRAY_TOOLTIP)
self.trayicon.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
self.trayicon.Bind(wx.EVT_MENU, self.OnShow, id=wx.ID_OPEN)
wxPython uses something called an AcceleratorTable for keeping track of "hotkeys" or "shortcuts" or whatever you'd like to call them. Similar to the way you can define and set a sizer, you can define and set an AcceleratorTable and wxPython will use it. More on that here http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/
Also, in order to get the Ctrl-Q or Ctrl-O hotkeys you need to either specify them in the text or add them to the accelerator table. If you add the properly formatted text to the menu items, wxPython should recognize "this is a hotkey" and add it to the accelerator table for you automatically.
self.menu.Append(wx.ID_OPEN, "Show\tCtrl+O")
self.menu.Append(wx.ID_EXIT, "Close\tCtrl+Q")
精彩评论