Lessons Learned : Linux System Tray Icons in python

While programming in python, for an Ubuntu/Linux Mint target, you may want to include a system tray icon for batter interaction with users. In Ubuntu, these are called “application indicators”. A basic appindicator icon is quite simple to do in python via PyGTK. There’s a very good example here:

http://developer.ubuntu.com/resources/technologies/application-indicators/

Scroll down to the PyGTK section and there’s a pretty self-explanatory example. I ran into two issues during the project:

  • I needed the appindicator to periodically run a function/method. This function would periodically check some flags, and then update it’s icon. Took some searching, but turns out this is possible via the glib module, and it’s timeout-add method, which you can read about here:

https://developer.gnome.org/pygobject/stable/glib-functions.html#function-glib–timeout-add

Using it is pretty simple. Make sure to include the module of course:

import glib

If you are using classes, as I did, add the following to your __init__ method:

glib.timeout_add(5000, self.some_function)

Where some_function is a method in your class which will be run every 5 seconds in my example above.

  • In pre 13.x ubuntu editions, applications needed to be whitelisted in order to give them permission to use the system tray. So you need to cater for this if you have users that may be using pre-13.x ubuntu versions. You can include the following in your install script (warning – the below whitelists every application):

release=`lsb_release -a 2>/dev/null| grep Release | awk ‘{print $2;}’`
if [[ $release < 13 ]] 

then 

        sudo gsettings set com.canonical.Unity.Panel systray-whitelist “[‘all’]” 

fi

  • If you need to specify custom icon paths, you may need to use absolute paths… in some cases relative paths simply didnt show any icons …

References:

https://unity.ubuntu.com/projects/appindicators/