AppIndicator3 - change icon

3.1k views Asked by At

I followed a tutorial to create a AppIndicator for Ubuntu.

I did what I wanted, but I got a strange behavior when I try to change the icon.

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

With that code, when I launch my indicator, the icon is white. Then when I click on "Change color", it's waiting 10 second then become red.

How could I change the icon to green, then to red with actions between the changes (here it's a sleep, but I'd like to launch other commands)

1

There are 1 answers

0
lapisdecor On

Im not sure about what you want to do but you can try this code:

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