在systray应用程序中的菜单项前面设置图标

时间:2017-03-10 00:30:16

标签: python linux systray

是否可以在下面的系统托盘应用中的菜单条目“顶部菜单,中间菜单,最后一个菜单”前面设置自定义图标?

#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
import sys

TRAY_TOOLTIP = 'Tray App'
TRAY_ICON = '/usr/share/icons/hicolor/32x32/apps/distributor.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)

    def CreatePopupMenu(self):
        menu = wx.Menu()

        # Top Menu
        create_menu_item(menu, 'Top Menu', self.TopMenu)
        menu.AppendSeparator()

        # Middle Menu
        create_menu_item(menu, 'Middle Menu', self.MiddleMenu)

        # exit and Info
        menu.AppendSeparator()
        create_menu_item(menu, 'Last Menu', self.LastMenu)

        create_menu_item(menu, 'Exit', self.ExitMenu)

        return menu

    def set_icon(self, path):
        icon = wx.IconFromBitmap(wx.Bitmap(path))
        self.SetIcon(icon, TRAY_TOOLTIP)

    def TopMenu(self, event):
        print 'This is from Top Menu!'

    def MiddleMenu(self, event):
        print 'This is from Middle Menu!'

    def LastMenu(self, event):
        print 'This is from Last Menu!'

    def ExitMenu(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()

我试图找到类似的东西,但我找不到任何相关的东西。我看到C#允许在菜单项前面设置图标,但我不确定它是否也适用于python。 感谢您的帮助和关注。

2 个答案:

答案 0 :(得分:0)

> sessionInfo() R version 3.3.2 (2016-10-31) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Debian GNU/Linux 8 (jessie) locale: [1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C [3] LC_TIME=en_US.utf8 LC_COLLATE=en_US.utf8 [5] LC_MONETARY=en_US.utf8 LC_MESSAGES=en_US.utf8 [7] LC_PAPER=en_US.utf8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] testpkg_0.0.0.9000 arm_1.9-3 lme4_1.1-12 [4] Matrix_1.2-8 MASS_7.3-45 roxygen2_6.0.1 [7] devtools_1.12.0.9000 loaded via a namespace (and not attached): [1] Rcpp_0.12.9 xml2_1.1.1 whisker_0.3-2 [4] magrittr_1.5 splines_3.3.2 pkgload_0.0.0.9000 [7] lattice_0.20-34 R6_2.2.0 minqa_1.2.4 [10] stringr_1.2.0 tools_3.3.2 grid_3.3.2 [13] pkgbuild_0.0.0.9000 nlme_3.1-131 coda_0.19-1 [16] git2r_0.18.0 withr_1.0.2 commonmark_1.2 [19] abind_1.4-5 rprojroot_1.2 digest_0.6.12 [22] assertthat_0.1 crayon_1.3.2 callr_1.0.0.9000 [25] nloptr_1.0.4 memoise_1.0.0 stringi_1.1.2 [28] compiler_3.3.2 desc_1.1.0 backports_1.0.5 提供wx.MenuItem作为方法...所以我很确定你可以称之为......

SetBitmap

答案 1 :(得分:0)

经过一些研究,试验和错误,我发现我必须在菜单定义中使用变量icon

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

并在菜单项的定义中包含图标的路径:

        create_menu_item(menu, 'Title', self.Title, icon='icon.png')

结果: enter image description here