简体   繁体   中英

wxpython systray icon menu

I'm designing an application that I want to run in the background. There isn't any user interaction necessary, so I want the app to run invisibly save for a systray icon. I want that icon to have a menu that just opens the config/help files in notepad. Could someone point me in the right direction or provide an example?

You can probably do this more cleanly but I have using some samples a while back I was able to create myself a class to handle the basic contruction of a taskbar icon.

TaskBarIcon.py

import wx


ID_SHOW_OPTION = wx.NewId()
ID_EDIT_OPTION = wx.NewId()


class Icon(wx.TaskBarIcon):

    def __init__(self, parent, icon, tooltip):
        wx.TaskBarIcon.__init__(self)
        self.SetIcon(icon, tooltip)
        self.parent = parent
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnLeftDClick)
        self.CreateMenu()

    def CreateMenu(self):
        self.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.OnPopup)
        self.menu = wx.Menu()
        self.menu.Append(ID_SHOW_OPTION, '&Show Option 1')
        self.menu.Append(ID_EDIT_OPTION, '&Edit Option 2')
        self.menu.AppendSeparator()
        self.menu.Append(wx.ID_EXIT, 'E&xit')

    def OnPopup(self, event):
        self.PopupMenu(self.menu)

    def OnLeftDClick(self, event):
        if self.parent.IsIconized():
            self.parent.Iconize(False)
        if not self.parent.IsShown():
            self.parent.Show(True)
        self.parent.Raise()

Within your Frame's init (), add the two lines below:

self.TrayIcon  = tbi.Icon(self, wx.Icon("C:\\YourIcon.png", wx.BITMAP_TYPE_PNG), "ToolTip Help Text Here")
self.Bind(wx.EVT_ICONIZE, self.OnIconify)

And now just add this function to your frame and you should be set:

def OnIconify(self, event):
    self.Hide()

Just remember to edit the items in the Icon class to suit your needs.

Have you considered running this application as a windows service? Many users will consider a system tray icon with little to no functionality a nuisance. You could still provide links to help/config files as a start menu entry.

The python win32 extensions package should have support for python services.

Of course, there are still reasons why you may want to run this as a system tray icon. I'm sorry that I don't have any experience with that.

You want the wx.TaskBarIcon:

http://docs.wxwidgets.org/stable/wx_wxtaskbaricon.html

The wxPython Demo has example code you can look at.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM