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()

对不起,我的英语不好。我想处理关闭 xfce4-notifyd 通知。在第一种情况下,函数“on_closed()”起作用。为什么在第二种情况下它不起作用?这只适用于一个命名空间?

4

1 回答 1

1

它不起作用,因为 Notification 对象在show_notification()返回并被释放时超出范围。您可以通过例如从函数返回 Notification 对象并将其存储在主体的变量中来使其工作。

于 2015-07-13T19:28:42.197 回答