AppIndicator3:从文件名或 GdkPixbuf 设置指示器图标

Sco*_*nce 5 indicator icons python

我正在编写一个小型 Python 3应用程序,它用于AppIndicator3在顶部栏中放置一个图标,然后根据用户操作更改该图标。很简单吧?由于该应用程序很小,因此需要从其源目录运行,无需任何安装。

问题是AppIndicator3.set_icon()需要str带有图标名称的 ,而不是所述图标的路径。

我怎样才能说服 AppIndicator3 让我给它一个文件名或一个Pixbuf? 或者,如何将我的图标目录添加到图标名称搜索路径?(我试过AppIndicator3.set_icon_theme_path(),但我的图标名称仍然无法识别。

Jac*_*ijm 5

使用图标的路径最好用一个例子来说明。在下面的示例中,我将图标保存在与脚本(指示器)相同的目录中,这在您的情况下似乎是一个方便的解决方案。

最重要的是,一旦您启动了指标:

class Indicator():
    def __init__(self):
        self.app = "<indicator_name>"
        iconpath = "/path/to/initial/icon/"

        -------------------------------

        self.testindicator = AppIndicator3.Indicator.new(
        self.app, iconpath,
        AppIndicator3.IndicatorCategory.OTHER)

        -------------------------------
Run Code Online (Sandbox Code Playgroud)

您可以使用以下方法更改图标:

        self.testindicator.set_icon("/path/to/new/icon/")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 在此处输入图片说明

例子

在下面的例子中,所有图标,nocolor.pngpurple.pnggreen.png与脚本存储在一起,但路径图标集

currpath = os.path.dirname(os.path.realpath(__file__))
Run Code Online (Sandbox Code Playgroud)

可以在任何地方。

class Indicator():
    def __init__(self):
        self.app = "<indicator_name>"
        iconpath = "/path/to/initial/icon/"

        -------------------------------

        self.testindicator = AppIndicator3.Indicator.new(
        self.app, iconpath,
        AppIndicator3.IndicatorCategory.OTHER)

        -------------------------------
Run Code Online (Sandbox Code Playgroud)

笔记

...如果您需要从第二个线程更新图标,则需要使用

GObject.threads_init()
Run Code Online (Sandbox Code Playgroud)

Gtk.main()
Run Code Online (Sandbox Code Playgroud)

并且您需要使用以下方法更新界面(图标或指示器文本):

GObject.idle_add()
Run Code Online (Sandbox Code Playgroud)

如此此处所应用。