我想在pygobject的通知中顯示按鈕。這個按鈕應該在點擊時調用一個回調函數,但它不會,我不明白爲什麼。未使用pygobject通知操作調用回叫
這裏是我的代碼:
from gi.repository import Notify, Gtk
class Test:
def __init__(self):
Notify.init('example')
self.notif()
Gtk.main()
def notif(self):
notif = Notify.Notification.new('Title', 'something','dialog-information')
notif.add_action('display', 'Button', self.callback, None)
notif.show()
def callback(self, notif_object, action_name, users_data):
print("Work!")
Gtk.main_quit()
Test()
當我點擊按鈕「按鈕」,沒有任何反應和回調不叫。 問題是什麼?
經過一番嘗試,我發現當我把Gtk.main()
緊接在notif.show()
後面時,回調工作。但是我不能使用這個解決方案,因爲它暗示我以後不能再顯示其他通知。
PyGObject 3.10.2+中實際不需要'Gdk.threads_init()'。你的例子是有效的,因爲當你調用'Gtk.main()'時你仍然持有'notification'的引用,並且不能解決OP所具有的問題。 – Fenikso 2015-09-29 08:40:10