[pygtk] Overriding interface properties

Arjan Molenaar gaphor at gmail.com
Mon Jun 20 04:24:18 WST 2011


On 17 Jun 2011, at 16:46, Just Fill Bugs wrote:

> On 06/17/2011 05:05 PM, Arjan Molenaar wrote:
>> 
>> On 17 Jun 2011, at 05:25, Just Fill Bugs wrote:
>> 
>>> On 06/17/2011 12:44 AM, Arjan Molenaar wrote:
>>>> Hi,
>>>> 
>>>> I've started porting Gaphas to GTK+ 3 now, using PyGI on Linux. It all goes quite smoothly. Only now I'm running
>>>> into the problem that my widget should implement the Gtk.Scrollable interface. For that it should define
>>>> a few properties.
>>>> 
>>>> In C those need to be declared using g_object_class_override_property(). What's the Python counterpart for this?
>>>> 
>>> 
>>> It seems the PyGobject examples/properties.py works okey once you replaced gobject with GObject to make it gi compatible. So take a look at the PyGobject's examples directory.
>> 
>> Did that. Only there the creation of new properties is demoed. I need to implement properties defined in an interface. For that the protocol is slightly different. I can't find how that is covered.
>> 
> 
> Aha, looking into pygobject source tree under the tests/test_properties.py, it shows how to do the getter/setter in the
> class TestProperty(unittest.TestCase):
> 
>    def testCustomGetter(self):
>        class C(gobject.GObject):
>            def get_prop(self):
>                return 'value'
>            prop = gobject.property(getter=get_prop)
> 
>        o = C()
>        self.assertEqual(o.prop, 'value')
>        self.assertRaises(TypeError, setattr, o, 'prop', 'xxx')
> 
>    def testCustomSetter(self):
>        class C(gobject.GObject):
>            def set_prop(self, value):
>                self._value = value
>            prop = gobject.property(setter=set_prop)
> 
>            def __init__(self):
>                self._value = None
>                gobject.GObject.__init__(self)
> 
>        o = C()
>        self.assertEquals(o._value, None)
>        o.prop = 'bar'
>        self.assertEquals(o._value, 'bar')
>        self.assertRaises(TypeError, getattr, o, 'prop')
> 
> So you have to specify getter/setter when declare properties at gobject.property().
> 
> Someone should make a better example for the new style property specification in the doc.

This looks nice and really Pythonic :)

But it's not a big help when implementing an interface :( (at least, that's what I see when implementing this). It seems odd to me to define the property again with type and all. 

Regards,

Arjan






More information about the pygtk mailing list