我按照教程为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秒然后变红。
如何将图标更改为绿色,然后更改为红色,并在更改之间执行操作(此处为“睡眠状态”,但我希望启动其他命令)
答案 0 :(得分: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()