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

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch