PyGTK 属性与 python 属性
当从 PyGTK 中的 GObject 类派生时,您可以像在 C 中一样定义 GObject 属性,使用 __gproperties__ 字典和 do_get_property
/do_set_property
方法,如 Python 中的 GObject 子类< /a>.请注意,这是在 Python 中使用 @property
装饰器之前编写的。
GObject 属性的优点是,您可以连接到对象的 notify::property-name
信号,以便在属性发生更改时接收通知。除此之外,是否有任何充分的理由使用 GObject 属性而不是 Python 的 @property 装饰器?
When deriving from a GObject class in PyGTK, you can define GObject properties like in C, using a __gproperties__
dict, and do_get_property
/do_set_property
methods, as described here in Sub-classing GObject in Python. Note that this was written before we had the @property
decorator in Python.
GObject properties have the advantage that you can connect to the object's notify::property-name
signal to receive a notification whenever the property changes. Other than that, is there any good reason to use GObject properties instead of Python's @property
decorator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用它们呢?使用由
Python 绑定、定义 GObject 属性与
定义 Python 属性。
话虽如此,除了优点之外,还有一些优点
通知。我过去使用过的就是财产
在 gtk.Builder 文件中设置。例如,在您的 UI 文件中,
当 Image 对象由
建造者类;如果您在中使用 GObject 属性
您的自定义小部件,您也可以利用这一点。这种行为将会是均匀的
Glade 中的属性绑定支持。
另一个潜在的优势是 GObject 属性的最小/最大限制和
整数和浮点数的默认值,有时很有用
UI 相关属性。
Why not use them? With the simplified version provided by the
Python bindings, defining GObject properties is not that different to
defining Python properties.
Having said that, there are a couple of advantages other than
notification. The one that I've made use of in the past is property
setting in gtk.Builder files. For example, in your UI file,
sets the
stock
property on the Image object when constructed by theBuilder class; if you use GObject properties in
your custom widgets, you can take advantage of this too. This behaviour will be even
more useful with the advent of property binding support in Glade.
Another potential advantage is GObject properties' min/max limits and
default values for integers and floats, which can sometimes be useful for
UI-related properties.
并不真地。它们有很多样板,所以除非我需要
notify
信号,否则我会用 Python 编写我需要的任何内容。话虽如此,我会认为“我不需要此属性的通知”,然后意识到“实际上,我根本不需要此属性”,这是相当常见的。
Not really. There's a lot of boilerplate for them, so unless I need the
notify
signals, I write whatever I need in Python.Having said that, it's fairly common that I'll think "I don't need notifications for this property" and then realise "actually, I don't need this property at all, then."