在 PyGTK/GObject 中使用枚举属性

发布于 2024-09-07 05:22:14 字数 1614 浏览 3 评论 0原文

本教程介绍如何在Python 仅涵盖使用 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')

我缺少什么?

This tutorial on using GObject in Python only covers using a property of type gobject.TYPE_FLOAT.

I've adapted it to use an enumerated type:

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

...but when I attempt to run it I get the following error:

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

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无需解释 2024-09-14 05:22:14

仅仅告诉 __gproperties__ 它是一个枚举类型还不够;您需要向 GObject 类型系统注册枚举,然后使用从中获取的 GType 值而不是 gobject.TYPE_ENUM。至少, C 语言就是这样实现的。我不确定 PyGTK 执行此操作的正确方法是什么,但它可能涉及编写 .defs 文件并在其上运行 pygobject-codegen-2.0 。

当然,将 gobject.TYPE_INT 类型的属性设置为与枚举范围匹配的最小值和最大值可能更容易,除非您确实需要 GObject系统了解您的枚举的详细信息。

It's not enough enough to tell __gproperties__ that it's an enumerated type; you need to register the enumeration with the GObject type system, and then use the GType value you get from that instead of gobject.TYPE_ENUM. At least, that's how it's done in C. I'm not sure what the proper way to do this is PyGTK is, but it might involve writing a .defs file and running pygobject-codegen-2.0 on it.

Of course, it's probably easier to just make the property of type gobject.TYPE_INT with a minimum and maximum value that matches the bounds of your enum, unless you really need the GObject system to understand the details of your enumeration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文