I'm trying to write a program with pynotify, the Python bindings for libnotify. I want to bring up a critical notification at some point and have it updated every few seconds as information changes, until the user clicks it away. This all works except handling what happens when the user dismisses it.
In order to update a notification I need to call Notification.show
after Notification.update
. That's fine, but it means that I need to keep track of whether the user has dismissed the notification, otherwise it'll just keep popping up again.
That ought to be possible by two methods I can think of:
- Detecting whether the notification is visible or not. I haven't found any way of finding that out.
- Storing some variable when the notification is closed, then checking it before updating and calling
Notification.show
again.
This second method should be possible. Example code I found (there doesn't seem to be any proper documentation for pynotify) led me to call Notification.connect
to connect a "closed"
signal to a callback. I tried to do that but the callback was never fired.
I Googled and debugged for a long time but couldn't make any progress. Eventually I found some example scripts which come with pynotify. One of them attaches a handler to the "closed"
signal: test-xy-stress.py
Its contents are as follows:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gobject
import gtk
import gtk.gdk
import pynotify
import sys
import random
exposed_signal_id = 0
count = 0
def handle_closed(n):
print "Closing."
def emit_notification(x, y):
n = pynotify.Notification("X, Y Test",
"This notification should point to %d, %d." % (x, y))
n.set_hint("x", x)
n.set_hint("y", y)
n.connect('closed', handle_closed)
n.show()
def popup_random_bubble():
display = gtk.gdk.display_get_default()
screen = display.get_default_screen()
screen_x2 = screen.get_width() - 1
screen_y2 = screen.get_height() - 1
x = random.randint(0, screen_x2)
y = random.randint(0, screen_y2)
emit_notification(x, y)
return True
if __name__ == '__main__':
if not pynotify.init("XY Stress"):
sys.exit(1)
gobject.timeout_add(1000, popup_random_bubble)
gtk.main()
I ran this and found that the callbacks here never fire either.
Could this be just my system, or is there a bug in pynotify or libnotify somewhere? If this is something beyond help right now, what about option 1 above -- is there any way to do that?
I seem to have libnotify 0.4.5 and pynotify 0.1.1.
I've been looking for the same thing. I found someone using gobject.MainLoop instead of gtk.main to help out: linuxquestions.org.
I found this worked for me: