how to set a menubar icon on mac osx using wx

2019-01-18 00:28发布

问题:

I could not find any pointers on how to create a menubar icon on OSX using wx. I originally thought that the wxTaskBarIcon class would do, but it actually creates an icon on the Dock. On Windows, wxTaskBarIcon creates a Systray icon and associated menu, and I would think that on mac osx it would create a menubar icon, I guess not.

回答1:

You have to set wxTaskBarIconType to STATUSITEM, not DOCK. The Cocoa APIs for this are NSStatusBar and NSStatusItem; here's the code in wxWidgets that calls to them.



回答2:

This post by Robin Dunn, the creator of wxPython, explains that wxPython doesn't support menubar icons on mac yet. They only support the Dock.



回答3:

As of wxPython 2.9.2.0 wx.TaskBarIcon will create a menubar icon now instead on OSX, so long as you call SetIcon.



回答4:

There is an example on wiki.wxpython.org that puts an icon in the "status menus" section (right-hand side) of the macOS menu bar (ignore the page title): https://wiki.wxpython.org/Custom%20Mac%20OsX%20Dock%20Bar%20Icon

It works for me with macOS High Sierra (10.13.3) running python 2.7.14 (installed using miniconda) with wxpython 3.0.0.0 osx-cocoa (classic).

Similarly, it works with python 3.6.4 and wxpython 4.0.1 osx-cocoa (phoenix); minor code changes required:

  • you must import wx.adv
  • wx.TaskBarIcon becomes wx.adv.TaskBarIcon
  • wx.IconFromBitmap becomes wx.Icon

This generates a status/notification/taskbar-type icon on other platforms as well.