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.pnggreen.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()

메모

...다음에서 아이콘을 업데이트해야 하는 경우두 번째 스레드, 당신은 사용해야합니다

GObject.threads_init()

~ 전에

Gtk.main()

다음을 사용하여 인터페이스(아이콘 또는 표시기 텍스트)를 업데이트해야 합니다.

GObject.idle_add()

예를 들어 적용됨여기그리고여기.

관련 정보