Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Python Notifier

You've probably noticed pop-up dialogues appearing when you have new mail in Thunderbird, or Dropbox has been updated etc. Every modern Linux desktop distribution appears to have these.

It's really easy to create these notifications in Python. You can even add icons to them, although this is rarely used.

The example below creates a class that can be used to create new notifications in the "GTK3 based programming style".

from gi.repository import GObject
from gi.repository import Notify

class SimpleNotifier(GObject.Object):

    def __init__(self):
        super(SimpleNotifier, self).__init__()
        # lets initialise with the application name
        Notify.init("myapp_name")

    def sendNotification(self, title, text, file_path_to_icon=""):
        n = Notify.Notification.new(title, text, file_path_to_icon)
        n.show()

my = SimpleNotifier()
my.sendNotification("this is a title", "this is some text")

Below is more procedural example.

from gi.repository import Notify

Notify.init("myapp_name")

title = "Title goes here"
text = "Body goes here"
icon_path = ""

notification = Notify.Notification.new(title, text, icon_path)
notification.show()

References

Last updated: 25th March 2021
First published: 16th August 2018