AppIndicator3: ファイル名または GdkPixbuf からインジケーターアイコンを設定する

AppIndicator3: ファイル名または GdkPixbuf からインジケーターアイコンを設定する

私は小さなPython 3を書いていますアプリこれを使用してAppIndicator3、トップバーにアイコンを配置し、ユーザーの操作に応じてそのアイコンを変更します。簡単ですよね? アプリは小さいため、インストールせずにソース ディレクトリから実行する必要があります。

問題はそれですAppIndicator3.set_icon()strアイコンへのパスではなく、アイコン名が必要です。

AppIndicator3 にファイル名または のいずれかを指定させるにはどうしたらよいでしょうPixbufか。あるいは、アイコン名の検索パスにアイコン ディレクトリを追加するにはどうすればよいでしょうか。( を試しましたAppIndicator3.set_icon_theme_path()が、アイコン名はまだ認識されません。

答え1

使用するにはパスアイコンへの追加は、例で説明するのが一番です。以下の例では、アイコンをスクリプト (インジケーター) と同じディレクトリに保存していますが、これはあなたのケースでは便利な解決策のようです。

肝心なのは、インジケーターを開始すると次のようになります。

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)

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

アイコンを変更するには:

        self.testindicator.set_icon("/path/to/new/icon/")

ここに画像の説明を入力してくださいここに画像の説明を入力してください

以下の例では、すべてのアイコン、、nocolor.pngpurple.pngスクリプトgreen.pngと一緒に保存されていますが、アイコンへのパスは、

currpath = os.path.dirname(os.path.realpath(__file__))

どこにでもあるかもしれない。

#!/usr/bin/env python3
import os
import signal
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3

currpath = os.path.dirname(os.path.realpath(__file__))

class Indicator():
    def __init__(self):
        self.app = 'show_proc'
        iconpath = currpath+"/nocolor.png"
        # after you defined the initial indicator, you can alter the icon!
        self.testindicator = AppIndicator3.Indicator.new(
            self.app, iconpath,
            AppIndicator3.IndicatorCategory.OTHER)
        self.testindicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)       
        self.testindicator.set_menu(self.create_menu())

    def create_menu(self):
        menu = Gtk.Menu()
        item_quit = Gtk.MenuItem(label='Quit')
        item_quit.connect('activate', self.stop)
        item_green = Gtk.MenuItem(label='Green')
        item_green.connect('activate', self.green)
        item_purple = Gtk.MenuItem(label='Purple')
        item_purple.connect('activate', self.purple)
        menu.append(item_quit)
        menu.append(item_green)
        menu.append(item_purple)
        menu.show_all()
        return menu

    def stop(self, source):
        Gtk.main_quit()

    def green(self, source):
        self.testindicator.set_icon(currpath+"/green.png")

    def purple(self, source):
        self.testindicator.set_icon(currpath+"/purple.png")

Indicator()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()

注記

...アイコンを更新する必要がある場合は、2番目のスレッド、使用する必要があります

GObject.threads_init()

前に

Gtk.main()

次を使用してインターフェース (アイコンまたはインジケーター テキスト) を更新する必要があります。

GObject.idle_add()

適用例ここそしてここ

関連情報