How to remove stale pynotify notification?

I just started with python and wrote myself a nice little script that uses gnome notifications via pynotify, for example:

import pynotify pynotify.init("Application") alert = pynotify.Notification("Title", "Description") alert.show(); 

This works fine, but the fact is that when I execute the script twice in a row, it takes some time for the first notification. After that, the second will be shown. Since the first one is deprecated when I run the script a second time, I want to remove the first program code first before showing the second (or replace it). Is this possible, and if so, how?

A little context to understand why I need it: since I often switch my mouse from left to right and vice versa, I need a script that simply inverts this preference and tells me in the notification “switched to left” and “switched to right”.

+7
source share
1 answer

I searched around for a while and came to the conclusion that in this case it is impossible .

You can use Notification.update() to update an existing notification object. But you cannot query existing ones from the system to modify or hide them. It may be possible to save the object somewhere through serialization and restore it for updating. But even then, you still need to know the exact duration of the notification and the timestamp when it starts, since there is no way to check if everything is visible.

A short example of using update() . Just for reference, as the pynotify doc seems almost nonexistent to me:

 #!/usr/bin/env python import pynotify pynotify.init("MyApplication") a = pynotify.Notification("Test notification", "Lorem ipsum op") a.show() raw_input("Press return to update the notification") a.update("Updated notification", "Ipsum lorem still op") a.show() 

You must call show() after the update. Otherwise, the changes will not be displayed.

The Notification object also has a close () function, but for me it does nothing (there may be a system dependency on Linux / Gnome).

+6
source

All Articles