2010-06-29 62 views
1

This tutorial關於在Python中使用GObject僅涵蓋使用gobject.TYPE_FLOAT類型的屬性。在PyGTK/GObject中使用枚舉屬性

我已經適應它使用一個枚舉類型:

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

FUEL_NONE = 0 
FUEL_SOME = 1 
FUEL_FULL = 2 

class Car(gobject.GObject): 
    __gproperties__ = { 
     'fuel' : (gobject.TYPE_ENUM,       # type 
       'fuel of the car',       # nick name 
       'amount of fuel that remains in the tank', # description 
       FUEL_SOME,         # default value 
       gobject.PARAM_READWRITE)     # flags 
    } 

# <<rest of demo code>> 

...但是當我嘗試運行它,我得到以下錯誤:

/usr/lib/pymodules/python2.5/gtk-2.0/gobject/__init__.py:114: Warning: g_param_spec_enum: assertion `g_enum_get_value (enum_class, default_value) != NULL' failed 
    type_register(cls, namespace.get('__gtype_name__')) 
Traceback (most recent call last): 
    File "gcar.py", line 9, in <module> 
    class Car(gobject.GObject): 
    File "/usr/lib/pymodules/python2.5/gtk-2.0/gobject/__init__.py", line 60, in __init__ 
    cls._type_register(cls.__dict__) 
    File "/usr/lib/pymodules/python2.5/gtk-2.0/gobject/__init__.py", line 114, in _type_register 
    type_register(cls, namespace.get('__gtype_name__')) 
TypeError: Error when calling the metaclass bases 
    could not create param spec for type GEnum (while registering property 'fuel' for GType '__main__+Car') 

我缺少什麼?

回答

1

它不足以告訴__gproperties__它是枚舉類型;您需要使用GObject類型系統註冊枚舉,然後使用從中獲取的GType值而不是gobject.TYPE_ENUM。至少,that's how it's done in C。我不確定PyGTK是做什麼的正確方法,但它可能涉及編寫一個.defs文件並在其上運行pygobject-codegen-2.0

當然,它可能更容易,只是讓gobject.TYPE_INT類型的屬性有最大值和最小值,你枚舉的界限匹配,除非你真的需要GObject的系統瞭解你的枚舉的細節。

+0

「......除非你真的需要GObject系統來理解你枚舉的細節......」 - 不,我不知道。它在那裏,使用它似乎是明智的,但「int」和我一樣好。 – detly 2010-06-29 03:45:49