简体   繁体   English

wxpython systray图标菜单

[英]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 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: 在框架的init ()中,添加以下两行:

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. 只需记住编辑Icon类中的项目即可满足您的需求。

Have you considered running this application as a windows service? 您是否考虑过将此应用程序作为Windows服务运行? 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. python win32扩展软件包应支持python服务。

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: 您需要wx.TaskBarIcon:

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

The wxPython Demo has example code you can look at. wxPython演示提供了示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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