How to program a status icon that will display in Ubuntu as well as in other distributions?

Solution 1:

I think that the need here is actually more determining of the python-appindicator library is present. If it is present, it will support all the fallback cases that you need. It will handle XFCE, KDE and older GNOME appropriately. Good example of how to do it in this answer.

The appindicator library will use DBus to check if the application indicator rendering process is available. This will be the case on Unity, or if the indicator-applet is running. If it is available it will use that, if not, it will fallback to using a GtkStatusIcon with the same menu.

Unfortunately, I believe you'd have to keep both code paths if you want to handle the case of the library not being available. Though, we'd be happy to help get the library in other distros :)

Solution 2:

I have an excellent solution that has worked well in StackApplet - I created a functionally-equivalent version of the appindicator module that uses gtk.StatusIcon internally to provide identical functionality when the real module does not exist.

Using it is as simple as:

  1. Downloading the following file and call it appindicator_replacement.py
  2. Adding the following to your application:

    try:
        import appindicator
    except ImportError:
        import appindicator_replacement as appindicator
    

That's it! Now your application will run perfectly with or without support for AppIndicators. It will even run on Windows, assuming you don't have any other platform-specific code.


Note: the file is released under the MIT license - so you can use it pretty much for anything.

Solution 3:

You will need to write code for both. You can detect the presence of appindicator in your Python code using something similar to:

have_appindicator = True
try:
    import appindicator
except:
    have_appindicator = False

From there, use have_appindicator to decide whether you should use appindicator code or gtk_status_icon code.

Unfortunately, this also means you will need to have both an Ubuntu and another environment to test with.