【问题标题】:Using enum properties in PyGTK/GObject在 PyGTK/GObject 中使用枚举属性
【发布时间】:2011-03-09 09:54:04
【问题描述】:

This tutorial 在 Python 中使用 GObject 仅涵盖使用 gobject.TYPE_FLOAT 类型的属性。

我已经对其进行了调整以使用枚举类型:

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')

我错过了什么?

【问题讨论】:

    标签: python pygtk gobject


    【解决方案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 系统来理解您枚举的详细信息。

    【讨论】:

    • "...除非您真的需要 GObject 系统来了解枚举的详细信息..." - 不,我不需要。它就在那里,使用它似乎很明智,但就我而言,int 和我一样好。
    猜你喜欢
    • 2011-02-16
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多