Search code examples
pythongtk3pygobject

Why does G_IS_OBJECT (object) fail for Gio.PropertyAction.new?


In a python3/pygobjects/gtk+3 application I’m trying to control a property through togglebuttons via Gio.PropertyAction. Unfortunately I don’t seem to be able to create the action.

I have boiled it down to this minimal example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio, Gtk, GObject

class Obj(GObject.GObject):
    prop = GObject.property(type = int)

    def __init__(self):
        action = Gio.PropertyAction.new('act', self, 'prop')

obj = Obj()

It yields this error:

(test.py:9099): GLib-GIO-CRITICAL **: g_property_action_new: assertion 'G_IS_OBJECT (object)' failed
Traceback (most recent call last):
  File "./test.py", line 11, in <module>
    obj = Obj()
  File "./test.py", line 9, in __init__
    action = Gio.PropertyAction.new('act', self, 'prop')
TypeError: constructor returned NULL

According to the Python GTK+ 3 Tutorial this is the correct way to handle properties. Curiously though, the Python GI API Reference does not know of GObject.GObject, but has only GObject.Object and requires it as second argument for Gio.PropertyAction.new (compare Gio.PropertyAction.new).

What am I doing wrong?


Solution

  • The mistake is, that I did not call the parent constructor, i.e. the one of GObject.GObject. The following works:

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gio, Gtk, GObject
    
    class Obj(GObject.GObject):
        prop = GObject.property(type = int)
    
        def __init__(self):
            super().__init__()
            action = Gio.PropertyAction.new('act', self, 'prop')
    
    obj = Obj()