Jump to content

[PyTeam] Desktop Notifications using libnotify


Recommended Posts

Vi este artigo sobre o uso da libnotify a partir do Python numa das minhas sessões de googling e achei que era mais que positivo postá-lo aqui. 😛

Desktop Notifications is a system for consolidating an API, UI and mechanism to allow applications, applets, services, etc. to notify the user when something interesting happens.

The software for this is currently hosted under the Galago project, which is listed on freedesktop.org.

This software consists of a notification-daemon, a client API implemented by the libnotify library, the DBus system for tying things together, and finally various language bindings (so far only python and of course C)

The simplest way to add notifications to your app is to call the notify-send utility. But this is quite limited, and so not very interesting.

To use libnotify in a python application do the following:

    import pynotify
    if pynotify.init("My Application Name"):
        n = pynotify.Notification("Title", "message")
        print "there was a problem initializing the pynotify module"
    print "you don't seem to have pynotify installed"

You can set the urgency level to one of three values using the following:


One option to the Notification() call is to add an icon. This can use one of three methods:

1. a URI specifying the icon file name (e.g. file://path/to/my-icon.png)

2. a 'stock' icon name. One that would succeed in a call to gtk_icontheme_lookup() (e.g. 'stock-delete') Note: these are not necessarily normal GTK stock icons - any theme icon will work.

3. a pixbuf

For the first two methods, just specify the icon name or URI as the 3rd parameter to the Notification() call.

        n = pynotify.Notification("Title", "message", "icon-name")

For the pixbuf method use: (where 'icon' is a pixbuf)


To set the timeout value for the displayed message:


To position the message (e.g. to associate it with a toolbar applet)

        n.attach_to_widget(widget) # 'self' often works
      # or position it explicitly
      #  n.set_hint("x", x-coordinate)
      #  n.set_hint("y", y-coordinate)

You can close the notification before it has timed out using


There's lots more you can do, such as putting buttons on the messages and getting callbacks when they are clicked, but that is beyond this tutorial. See the examples that come with the pynotify package to learn more.

Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum!

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.