2015-07-13 53 views
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()”起作用。为什么在第二种情况下它不起作用? 这只适用于一个命名空间?我想处理关闭xfce4-notifyd通知

回答

1

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