0

我按照教程为 Ubuntu 创建了一个 AppIndicator。

我做了我想做的事,但是当我尝试更改图标时出现了奇怪的行为。

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 秒然后变成红色。

如何将图标更改为绿色,然后更改为红色,并在更改之间进行操作(这里是睡眠,但我想启动其他命令)

4

1 回答 1

1

我不确定你想做什么,但你可以试试这个代码:

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()
于 2019-01-01T13:59:09.133 回答