Search code examples
pythonpython-3.xnotificationsgtk3pygobject

PyGObject Progress Bar in Notify.Notification


I am using PyGObject with Python 3 and I want to display a notification using a Notify.Notification which has a progress bar in it. The progress bar doesn't need to update on its own/asynchronously or anything - it can (and should) be static, updating only when I manually set a new value then tell the notification to show. I'm after something like a volume notification showing the new volume as you change it.

I have been unable to find any way to do this searching documentation such as this, is this possible using PyGObject? Alternatively is there another Python 3 library which would allow this behaviour?

I'm currently showing notifications with text-based progress similar to this:

import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

def __init___(self):
    ...
    Notify.init("Progress")
    self.notification = Notify.Notification(summary='Progress', body='0%')
    self.notification.set_image_from_pixbuf(notification_image)
    ...

def on_progress_update(self, progress):
    ...
    self.notification.update('Progress', str(progress) + '%', None)
    self.notification.show()
    ...

Solution

  • So after much more searching I found this thread on the xfce forums discussing the use of send-notify to get xfce4-notifyd "gauges", and was able to figure out how to use Notify.Notification.set_hint(). So, if you want your notification to show a progress bar/status bar/gauge, you can use the following:

    import gi
    
    gi.require_version('Notify', '0.7')
    from gi.repository import GLib, Notify
    
    Notify.init("Name")
    notification = Notify.Notification()
    notification.set_hint('value', GLib.Variant.new_int32(volume))
    

    Points of note:

    • Setting a summary and body, as seen in the original question, seems to be pointless if using this method since neither are shown, only the progress bar. An icon/image can still be shown along side the progress bar, e.g. with notification.set_image_from_pixbuf()
    • The GLib.Variant type can be int32, int64, double, and possibly others of numeric type larger than int32, but NOT byte or int16, any uint type such as uint32, or (I would assume) any non-numeric type. Just use int32 as fas I can tell.
    • While I was using generic names like Name for other steps, the value for set_hint MUST be 'value' with a lower case v.

    Also worth noting I'm not sure if this is a unniversal method or if it only works for xfce4-notifyd, I only use xfce4-notifyd so for now I'm not concerned, but if I look into this I will try to remember to update my answer. Alternatively if anyone else knows the answer to this please let me know.