AppIndicator3 - изменить значок

Я следовал руководству по созданию AppIndicator для Ubuntu.

Я сделал то, что хотел, но у меня странное поведение, когда я пытался сменить значок.

import gi
import os
import signal
import time

gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
gi.require_version('Notify', '0.7')

from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Notify as notify

APPINDICATOR_ID = 'testindicator'

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

class Indicator():
    def __init__(self):
        self.indicator = appindicator.Indicator.new(APPINDICATOR_ID, CURRPATH+"/white.svg", appindicator.IndicatorCategory.SYSTEM_SERVICES)
        self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
        self.indicator.set_menu(self.build_menu())
        notify.init(APPINDICATOR_ID)

    def build_menu(self):
        menu = gtk.Menu()

        item_color = gtk.MenuItem('Change color')
        item_color.connect('activate', self.change_color)

        item_quit = gtk.MenuItem('Quit')
        item_quit.connect('activate', self.quit)

        menu.append(item_color)
        menu.append(item_quit)
        menu.show_all()
        return menu

def change_color(self, source):
    time.sleep(5)
    self.indicator.set_icon(CURRPATH+"/green.svg")
    time.sleep(5)
    self.indicator.set_icon(CURRPATH+"/red.svg")

    def quit(self, source):
        gtk.main_quit()


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

С этим кодом, когда я запускаю свой индикатор, значок становится белым. Затем, когда я нажимаю "Изменить цвет", он ждет 10 секунд, а затем становится красным.

Как я могу изменить значок на зеленый, затем на красный с действиями между изменениями (здесь это сон, но я хотел бы запустить другие команды)

0 ответов

Я не уверен, что вы хотите сделать, но вы можете попробовать этот код:

import gi
import os
import signal
import time

gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
gi.require_version('Notify', '0.7')

from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Notify as notify

APPINDICATOR_ID = 'testindicator'

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

class Indicator():
    def __init__(self):
        self.indicator = appindicator.Indicator.new(APPINDICATOR_ID, CURRPATH+"/white.svg", appindicator.IndicatorCategory.SYSTEM_SERVICES)
        self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
        self.indicator.set_menu(self.build_menu())
        notify.init(APPINDICATOR_ID)

    def build_menu(self):
        menu = gtk.Menu()

        item_color = gtk.MenuItem('Change green')
        item_color.connect('activate', self.change_green)

        item_color2 = gtk.MenuItem('Change red')
        item_color2.connect('activate', self.change_red)

        item_quit = gtk.MenuItem('Quit')
        item_quit.connect('activate', self.quit)

        menu.append(item_color)
        menu.append(item_color2)
        menu.append(item_quit)
        menu.show_all()
        return menu

    def change_green(self, source):
        self.indicator.set_icon(CURRPATH+"/green.svg")

    def change_red(self, source):
        self.indicator.set_icon(CURRPATH+"/red.svg")

    def quit(self, source):
        gtk.main_quit()


Indicator()
signal.signal(signal.SIGINT, signal.SIG_DFL)
gtk.main()
Другие вопросы по тегам