Search code examples
pythonpygobjectgobject-introspectionlibnotify

Callback not called in pygobject notification action


I'd like to add an action to my Notification with a callback. I'm using pygobject with the following code:

import logging
from time import sleep

import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

def callback(*args, **kwargs):
    print("Got callback")
    print(locals())


def main():
    Notify.init("Hello World")
    notification = Notify.Notification.new("Testing")
    notification.add_action("my action", "Submit", callback)
    notification.show()
    sleep(5)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main()

When I run the script, I see the notification with the "Submit" button, but when I click the button the callback isn't run (as far as I can tell).

When I use ipython to inspect things, I get this help for add_action:

In [65]: Notify.Notification.add_action?
Type:        FunctionInfo
String form: gi.FunctionInfo(add_action)
File:        /usr/lib/python3.5/site-packages/gi/__init__.py
Docstring:   add_action(self, action:str, label:str, callback:Notify.ActionCallback, user_data=None)

So I see that the callback should be an ActionCallback? I then inspect that class:

In [67]: Notify.ActionCallback
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-67-aa40d4997598> in <module>()
----> 1 Notify.ActionCallback

/usr/lib/python3.5/site-packages/gi/module.py in __getattr__(self, name)
    231             wrapper = info.get_value()
    232         else:
--> 233             raise NotImplementedError(info)
    234
    235         # Cache the newly created wrapper which will then be

NotImplementedError: gi.CallbackInfo(ActionCallback)

...and I get a NotImplementedError. So are notification actions just not implemented in PyGObject? Or am I doing something wrong in passing my callback to the add_action method?

I'm on arch linux, using the package python-gobject 3.22.0-1, running with python 3.5.2.


Solution

  • It turns out I needed to run the Gtk main loop:

    from gi.repository import Gtk
    Gtk.main()
    

    Then the callback was called just fine