AppIndicator3更改图标

2024-06-16 10:42:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我跟随一个教程为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秒然后变成红色。在

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


Tags: importselfgtksignaldefitemquitindicator
1条回答
网友
1楼 · 发布于 2024-06-16 10:42:07

我不确定您想做什么,但您可以尝试以下代码:

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

相关问题 更多 >