我想处理关闭xfce4-notifyd通知

时间:2015-07-13 09:28:30

标签: python gtk gobject xfce pynotify

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()”起作用。为什么在第二种情况下它不起作用? 这只适用于一个命名空间?

1 个答案:

答案 0 :(得分:1)

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