Saturday, September 22, 2012

Unity Application Indicators using Python 3

Unity replaced the old mess of confusing and inconsistent individual notifications with a standardized set of Application Indicators, each at the head of a customized menu.

Here's how to create your own Indicator and associated menu using Python 3.


Prerequisite packages: python3-gi

sudo apt-get install python3-gi

Documentation and API reference for python 3 and GTK+ 3.
Documentation for the Unity-specific AppIndicator class.


Hello World: Let's use the test script at developer.ubuntu.com. There are two python scripts, use the PyGI. The other, PyGTK, is deprecated and Gtk is now included in PyGI.

The Hello World script is a bit long, so I won't copy it here. But it's easy enough to cut-and-paste.

The key points are:

1) Create an Application Indicator object:
      id = "Name of this client application"
      icon_name = "icon-name"                   Icons are in /usr/share/icons/ubuntu-mono-*-16
      category = appindicator.IndicatorCategory.APPLICATION_STATUS      List of categories
      indicator = appindicator.Indicator.new (id, icon_name, category)

2) Create the associated menu:

    menu = Gtk.Menu()
    item = Gtk.MenuItem("Menu Item #1")
    menu.append(item)
    item.show()
    item = Gtk.MenuItem("Menu Item #2")
    menu.append(item)
    item.show()

3) Attach the menu to the Application Indicator object

    indicator.set_menu(menu)

4) Make it work

    Gtk.main()









No comments: