【问题标题】:Callback not called with a pygobject notification action未使用 pygobject 通知操作调用回调
【发布时间】:2015-02-11 18:15:13
【问题描述】:

我想用 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() 之后时,回调起作用了。但是我不能使用这个解决方案,因为这意味着我以后不能显示其他通知。

【问题讨论】:

    标签: python notifications pygobject


    【解决方案1】:

    在调用回调之前,您需要保持对通知对象的引用:

    from gi.repository import Gtk, Notify
    
    
    class Window(Gtk.Window):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            Notify.init('Test')
            self.notification = None
    
            self.set_border_width(5)
    
            self.button = Gtk.Button('Test')
    
            self.box = Gtk.Box()
            self.box.pack_start(self.button, True, True, 0)
            self.add(self.box)
    
            self.button.connect('clicked', self.on_button)
            self.connect('delete-event', Gtk.main_quit)
            self.show_all()
    
        def on_button(self, button):
            if self.notification:
                self.notification.close()
            self.notification = Notify.Notification.new('Test')
            self.notification.add_action('clicked', 'Action', self.callback)
            self.notification.show()
    
        def callback(self, notification, action_name):
            print(action_name)
    
    win = Window()
    Gtk.main()
    

    如果您需要显示更多相同的通知,您需要一个通知对象列表。

    有关无窗口示例,请参阅answer

    【讨论】:

      【解决方案2】:

      更新

      看来你不用打电话了

      Gdk.threads_init()
      

      不记得我测试这个的情况,但可以发誓这对我来说很重要。

      更新示例:

      import sys
      
      from gi.repository import Notify
      from gi.repository import Gtk
      
      if not Notify.init('Notification Test'):
          print("ERROR: Could not init Notify.")
          sys.exit(1)
      
      notification = Notify.Notification.new(
          "Notification Title",
          "Message...")
      
      notification.set_urgency(Notify.Urgency.NORMAL)
      def actionCallback(notification, action, user_data = None):
          print("Callback called:"+action)
          Gtk.main_quit()
      
      notification.add_action("test-action", "Test Action", actionCallback)
      
      if not notification.show():
          print("ERROR: Could not show notification.")
          sys.exit(2)
      
      Gtk.main()
      

      【讨论】:

      • Gdk.threads_init() 实际上在 PyGObject 3.10.2+ 中是不需要的。您的示例有效,因为您在调用 Gtk.main() 时仍然持有对 notification 的引用,并且没有解决 OP 遇到的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      相关资源
      最近更新 更多