Quick and easy: trayicon with python?

前端 未结 9 1857
一生所求
一生所求 2020-11-28 03:27

I\'d just need a quick example on how to easily put an icon with python on my systray. This means: I run the program, no window shows up, just a tray icon (I\'ve got a png f

相关标签:
9条回答
  • 2020-11-28 03:31

    wx.PySimpleApp deprecated, here's how to use wx.App instead

    Took me while to figure this out so I thought I'd share. wx.PySimpleApp is deprecated in wxPython 2.9 and beyond. Here's FogleBird's original script using wx.App instead.

    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, frame):
            self.frame = frame
            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)
            self.frame.Close()
    
    class App(wx.App):
        def OnInit(self):
            frame=wx.Frame(None)
            self.SetTopWindow(frame)
            TaskBarIcon(frame)
            return True
    
    def main():
        app = App(False)
        app.MainLoop()
    
    
    if __name__ == '__main__':
        main()
    
    0 讨论(0)
  • 2020-11-28 03:31

    An alternative if you are trying to run a python based program in the background you can run it as a service. Check out this active state recipe its pretty useful. I believe one of the options is to convert your application to exe with py2exe or pyinstall.

    http://code.activestate.com/recipes/551780/

    0 讨论(0)
  • 2020-11-28 03:32

    For Ubuntu

    class TrayIcon:
        def init():
    
    
    iconPath = {"Windows":os.path.expandvars("%PROGRAMFILES%/MyProgram/icon.png"),
                      "Linux":"/usr/share/icons/myprogramicon.png"}        
        if platform.system()=="Linux":
            import gtk
            import appindicator # Ubuntu apt-get install python-appindicator 
    
        # Create an application indicator
        try:
            gtk.gdk.threads_init()
            gtk.threads_enter()
            icon = iconPath[platform.system()]
            indicator = appindicator.Indicator("example-simple-client", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
            indicator.set_icon(icon)
            indicator.set_status (appindicator.STATUS_ACTIVE)
            indicator.set_attention_icon ("indicator-messages-new")
            menu = gtk.Menu()
    
            menuTitle = "Quit"   
            menu_items = gtk.MenuItem(menuTitle)
            menu.append(menu_items)
            menu_items.connect("activate", TrayIcon.QuitApp, menuTitle)
            menu_items.show()
    
            menuTitle = "About My Program"
            menu_items = gtk.MenuItem(menuTitle)
            menu.append(menu_items)
            menu_items.connect("activate", TrayIcon.AboutApp, menuTitle)
            menu_items.show()   
    
            indicator.set_menu(menu)    
        except:
            pass
    
        # Run the app indicator on the main thread.
        try:
    
            t = threading.Thread(target=gtk.main)
            t.daemon = True # this means it'll die when the program dies.
            t.start()
            #gtk.main()
    
        except:
            pass
        finally:
            gtk.threads_leave()     
    
    @staticmethod
    def AboutApp(a1,a2):
        gtk.threads_enter()
        dialog = gtk.Dialog("About",
                            None,
                            gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                            (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
        label = gtk.Label("My Program v0.0.1, (C)opyright ME 2015. All rights reserved.")
        dialog.vbox.pack_start(label)
        label.show()
        label2 = gtk.Label("example.com\n\nFor more support contact me@gmail.com")
        label2.show()
        dialog.action_area.pack_end(label2)
        response = dialog.run()
        dialog.destroy()
        gtk.threads_leave()
    
    @staticmethod
    def QuitApp(a1, a2):
        sys.exit(0)
    

    Cross-Platform

    See PyQt: Show menu in a system tray application

    0 讨论(0)
  • 2020-11-28 03:36

    For an example, refer to this thread -> wx question.

    wxPython "classic" -> [new API] wxPython 'Phoenix' (Py3)

    0 讨论(0)
  • 2020-11-28 03:38

    Three is a package called pystray(bad name, just say it out loud) but works like a charm and is more lightweight than wx ot qt. These are the links
    https://pystray.readthedocs.io/en/latest/index.html
    https://pypi.org/project/pystray/

    0 讨论(0)
  • 2020-11-28 03:43

    For Windows & Gnome

    Here ya go! wxPython is the bomb. Adapted from the source of my Feed Notifier application.

    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)
    
    
    def main():
        app = wx.PySimpleApp()
        TaskBarIcon()
        app.MainLoop()
    
    
    if __name__ == '__main__':
        main()
    
    0 讨论(0)
提交回复
热议问题