Quick and easy: trayicon with python?

前端 未结 9 1858
一生所求
一生所求 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:43

    If you can guarantee windows and you do not want to introduce the heavy dependencies of wx, you can do this with the pywin32 extensions.

    Also see this question.

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

    2018 version

    import wx.adv
    import wx
    TRAY_TOOLTIP = 'Name' 
    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.Append(item)
        return item
    
    
    class TaskBarIcon(wx.adv.TaskBarIcon):
        def __init__(self, frame):
            self.frame = frame
            super(TaskBarIcon, self).__init__()
            self.set_icon(TRAY_ICON)
            self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
    
        def CreatePopupMenu(self):
            menu = wx.Menu()
            create_menu_item(menu, 'Site', self.on_hello)
            menu.AppendSeparator()
            create_menu_item(menu, 'Exit', self.on_exit)
            return menu
    
        def set_icon(self, path):
            icon = wx.Icon(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:54

    Yes. There is a cross-platform example on wiki.wxpython.org that I've tested with python 2.7 (minconda install) on macOS High Sierra (10.13.3), Windows 7, and gnome 3/centos7. It is here (ignore the page title): https://wiki.wxpython.org/Custom%20Mac%20OsX%20Dock%20Bar%20Icon

    Small mods are needed for python 3.6:

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

    Gnome 3 required installation of TopIcons Plus.

    Since you don't want to have the window display (" no window shows up, just a tray icon"), simply comment out the following line (though you still want to keep the wx.Frame parent):

    frame.Show(True)
    

    And since you want to use your own .png icon, remove the WXPdemo image and embeddedimage stuff and replace

    icon = self.MakeIcon(WXPdemo.GetImage())
    

    with, for example

    icon = wx.Icon('icon.png')
    

    In my experience, this will provide a good start for adapting or extending further.

    0 讨论(0)
提交回复
热议问题