Can I create a wxPython tray icon application without the Python icon appearing in the Dock?

三世轮回 提交于 2019-12-10 11:27:15

问题


Here is the slightly modified source code for an example tray icon application I found on SO:

import wx

TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = 'icon.png'

def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    menu.AppendItem(item)
    return item

class TaskBarIcon(wx.TaskBarIcon):
    def __init__(self):
        super(TaskBarIcon, self).__init__()
        self.set_icon(TRAY_ICON)
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
    def CreatePopupMenu(self):
        menu = wx.Menu()
        create_menu_item(menu, 'Say Hello', self.on_hello)
        menu.AppendSeparator()
        create_menu_item(menu, 'Exit', self.on_exit)
        return menu
    def set_icon(self, path):
        icon = wx.IconFromBitmap(wx.Bitmap(path))
        self.SetIcon(icon, TRAY_TOOLTIP)
    def on_left_down(self, event):
        print 'Tray icon was left-clicked.'
    def on_hello(self, event):
        print 'Hello, world!'
    def on_exit(self, event):
        wx.CallAfter(self.Destroy)

class App(wx.App):
    def OnInit(self):
        self.SetTopWindow(wx.Frame(None, -1))
        TaskBarIcon()

        return True

def main():
    app = App()
    app.MainLoop()

if __name__ == '__main__':
    main()

It shows a tray icon successfully, and indeed shows no Frame, but it does show the Python icon in the Dock. I assume it will also show a Python icon in the Windows task bar. How do I stop that from happening?

I'm using wxPython 2.9 on Python 2.7 on OSX Mountain Lion



回答1:


You should create a bundle from Your application and tell specify that your app is kind of "background", so it shall not show icon in dock.

To do this, You have to following property to Your bundle property file (Info.plist).

<key>LSUIElement</key>
<string>1</string>

See following articles:

  • how to create app bundle: Building OSX App Bundle
  • how hide app from "dock": http://www.macosxtips.co.uk/index_files/disable-the-dock-icon-for-any-application.php

  • Apple reference about launch properties of application: http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html



来源:https://stackoverflow.com/questions/13069452/can-i-create-a-wxpython-tray-icon-application-without-the-python-icon-appearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!