0

我想使用单个指标类创建两个或更多面板指标。这是代码:

#!/usr/bin/env python
import os
from gi.repository import Gtk
from gi.repository import AppIndicator3

class IndicatorObject:
      def create_indicator(self, indicator_id):
          indicator = AppIndicator3.Indicator.new(indicator_id, os.path.abspath('sample_icon.svg'), AppIndicator3.IndicatorCategory.SYSTEM_SERVICES)
          indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
          menu = Gtk.Menu()
          item_quit = Gtk.MenuItem('Quit')
          menu.append(item_quit)
          indicator.set_menu(menu)
          menu.show_all()
          print indicator_id

indicator1 = IndicatorObject()
indicator1.create_indicator("first_indicator")

indicator2 = IndicatorObject()
indicator2.create_indicator("second_indicator")
Gtk.main()

但是当我运行脚本时没有任何反应。如何使用单个类创建两个或多个指标?

4

1 回答 1

2
#!/usr/bin/env python
import os
from gi.repository import Gtk
from gi.repository import AppIndicator3

class AppIndicatorExample:
    def __init__(self, indicator_id):
        self.ind = AppIndicator3.Indicator.new(str(indicator_id), os.path.abspath('sample_icon.svg'), AppIndicator3.IndicatorCategory.SYSTEM_SERVICES)
        self.ind.set_status(AppIndicator3.IndicatorStatus.ACTIVE)

        # create a menu
        self.menu = Gtk.Menu()

        item = Gtk.MenuItem(str(indicator_id))
        item.show()
        self.menu.append(item)

        image = Gtk.ImageMenuItem(Gtk.STOCK_QUIT)
        image.connect("activate", self.quit)
        image.show()
        self.menu.append(image)

        self.menu.show()

        self.ind.set_menu(self.menu)

    def quit(self, widget, data=None):
        Gtk.main_quit()

if __name__ == "__main__":
    indicator = AppIndicatorExample(1)
    indicator2 = AppIndicatorExample(2)
    indicator3 = AppIndicatorExample(3)
    indicator4 = AppIndicatorExample(4)
    indicator5 = AppIndicatorExample(5)
    indicator6 = AppIndicatorExample(6)
    indicator7 = AppIndicatorExample(7)
    indicator8 = AppIndicatorExample(8)

    Gtk.main()

这是使用单个类创建一个或多个应用程序的正确方法。

于 2015-07-22T08:40:04.213 回答