0
import pynotify
import gobject


def on_clicked(notification, signal_text):
   print "1: " + str(notification)
   print "2: " + str(signal_text)
   notification.close()


def on_closed(notification):
   print "on_closed"
   notification.close()


def show_notification(title, body):
   n = pynotify.Notification(title, body)
   n.add_action("button", "Test button", on_clicked)
   n.connect("closed", on_closed)
   n.show()


if __name__ == '__main__':
   pynotify.init('TestApp')

   global loop
   loop = gobject.MainLoop()

   # first case
   notify = pynotify.Notification("1_notify", "test")
   notify.add_action("button", "Test button", on_clicked)
   notify.connect("closed", on_closed)
   notify.show()

   # second case
   show_notification("2_notify", "test")

   loop.run()

Sorry for my bad English. I want to handle closing xfce4-notifyd notification. In the first case, the function "on_closed()" works. Why in the second case it does not work? This only works well in one namespace?

1 Answers1

1

It does not work because the Notification object goes out of scope when show_notification() returns and is freed. You can make it work by e.g. returning the Notification object from the function and storing it in a variable in main body.

Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54