Hi Lorenzo,

I also struggled with subclassing Gobject recently and I posted my
experiences to this list to share them.
You can find my posting here:
http://www.daa.com.au/pipermail/pygtk/2003-August/005609.html

Hope this helps,

Jan

Lorenzo Gil Sanchez schrieb:
Hi,

I'm trying to understand how to create a custom subclass of
gobject.GObject and now I'm working with the properties stuff:

import pygtk
pygtk.require('2.0')
import gobject

class MyObject(gobject.GObject):
__gproperties__ = {
'prop_name' : (gobject.TYPE_STRING, 'short_description',
'long_description', 'initial_value',
gobject.PARAM_READABLE)
}


    def __init__(self):
        self.__gobject_init__()
        self.prop_name = 'initial_value'  # [1]

    def do_get_property(self, pspec):
        if pspec.name == 'prop-name':  # [2]
            return self.prop_name
        else:
            raise AttributeError, 'unknown property %s' % pspec.name

def do_set_property(self, pspec, value):
if pspec.name == 'prop-name':
self.prop_name = value
else:
raise AttributeError, 'unknown property %s' % pspec.name
gobject.type_register(MyObject)


print 'MyObject properties:', gobject.list_properties(MyObject)
o1 = MyObject()
print o1.get_property('prop-name')
o1.set_property('prop-name', 'other value')  # [3]
print o1.get_property('prop-name')


And here are my questions:


[1] No matter if I create a gobject property I still need to create a
Python property to 'hold' the actual data. Is this correct?

[2] If my property is called prop_name, PyGTK internally converts it to
prop-name so if I do o1.get_property('prop_name') I get an error. If I
have to create a instance variable like self.prop_name I can't create
code that make this two sentences equivalent:

print o1.prop_name
print o1.get_property('prop_name')  # this raises an error

[3] If I have set the flags for the property as gobject.PARAM_READABLE
why this statement is not raising an error or even a warning?:

o1.set_property('prop-name', 'other value')

Do I have to take this in consideration when writing the do_set_property
method?


I don't know if I'm doing it right but so far I don't find gobjects properties very useful when using them from Python. And I think what people actually dislikes most is the fact that you have to call gobject.type_register with your class, which is pretty ok with me.




_______________________________________________ pygtk mailing list [EMAIL PROTECTED] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to