| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
========== |
| 4 |
Properties |
| 5 |
========== |
| 6 |
|
| 7 |
Properties are part of a class and are defined through a |
| 8 |
:obj:`GObject.ParamSpec`, which contains the type, name, value range and so |
| 9 |
on. |
| 10 |
|
| 11 |
To find all the registered properties of a class you can use the |
| 12 |
:meth:`GObject.Object.list_properties` class method. |
| 13 |
|
| 14 |
.. code:: pycon |
| 15 |
|
| 16 |
>>> Gio.Application.list_properties() |
| 17 |
[<GParamString 'application-id'>, <GParamFlags 'flags'>, <GParamString |
| 18 |
'resource-base-path'>, <GParamBoolean 'is-registered'>, <GParamBoolean |
| 19 |
'is-remote'>, <GParamUInt 'inactivity-timeout'>, <GParamObject |
| 20 |
'action-group'>, <GParamBoolean 'is-busy'>] |
| 21 |
>>> param = Gio.Application.list_properties()[0] |
| 22 |
>>> param.name |
| 23 |
'application-id' |
| 24 |
>>> param.owner_type |
| 25 |
<GType GApplication (94881584893168)> |
| 26 |
>>> param.value_type |
| 27 |
<GType gchararray (64)> |
| 28 |
>>> |
| 29 |
|
| 30 |
The :obj:`GObject.Object` contructor takes multiple properties as keyword |
| 31 |
arguments. Property names usually contain "-" for seperating words. In Python |
| 32 |
you can either use "-" or "_". In this case variable names don't allow "-", so |
| 33 |
we use "_". |
| 34 |
|
| 35 |
.. code:: pycon |
| 36 |
|
| 37 |
>>> app = Gio.Application(application_id="foo.bar") |
| 38 |
|
| 39 |
To get and set the property value see :meth:`GObject.Object.get_property` and |
| 40 |
:meth:`GObject.Object.set_property`. |
| 41 |
|
| 42 |
.. code:: pycon |
| 43 |
|
| 44 |
>>> app = Gio.Application(application_id="foo.bar") |
| 45 |
>>> app |
| 46 |
<Gio.Application object at 0x7f7499284fa0 (GApplication at 0x564b571e7c00)> |
| 47 |
>>> app.get_property("application_id") |
| 48 |
'foo.bar' |
| 49 |
>>> app.set_property("application_id", "a.b") |
| 50 |
>>> app.get_property("application-id") |
| 51 |
'a.b' |
| 52 |
>>> |
| 53 |
|
| 54 |
|
| 55 |
Each instance also has a ``props`` attribute which exposes all properties |
| 56 |
as instance attributes: |
| 57 |
|
| 58 |
.. code:: pycon |
| 59 |
|
| 60 |
>>> from gi.repository import Gtk |
| 61 |
>>> button = Gtk.Button(label="foo") |
| 62 |
>>> button.props.label |
| 63 |
'foo' |
| 64 |
>>> button.props.label = "bar" |
| 65 |
>>> button.get_label() |
| 66 |
'bar' |
| 67 |
>>> |
| 68 |
|
| 69 |
|
| 70 |
To track changes of properties, :obj:`GObject.Object` has a special ``notify`` |
| 71 |
signal with the property name as the detail string. Note that in this case you |
| 72 |
have to give the real property name and replacing "-" with "_" wont work. |
| 73 |
|
| 74 |
.. code:: pycon |
| 75 |
|
| 76 |
>>> app = Gio.Application(application_id="foo.bar") |
| 77 |
>>> def my_func(instance, param): |
| 78 |
... print("New value %r" % instance.get_property(param.name)) |
| 79 |
... |
| 80 |
>>> app.connect("notify::application-id", my_func) |
| 81 |
11L |
| 82 |
>>> app.set_property("application-id", "something.different") |
| 83 |
New value 'something.different' |
| 84 |
>>> |
| 85 |
|
| 86 |
You can define your own properties using the :obj:`GObject.Property` decorator, |
| 87 |
which can be used similarly to the builtin Python :any:`property` decorator: |
| 88 |
|
| 89 |
.. function:: GObject.Property(type=None, default=None, nick='', blurb='', \ |
| 90 |
flags=GObject.ParamFlags.READWRITE, minimum=None, maximum=None) |
| 91 |
|
| 92 |
:param GObject.GType type: Either a GType, a type with a GType or a |
| 93 |
Python type which maps to a default GType |
| 94 |
:param object default: A default value |
| 95 |
:param str nick: Property nickname |
| 96 |
:param str blurb: Short description |
| 97 |
:param GObject.ParamFlags flags: Property configuration flags |
| 98 |
:param object minimum: Minimum value, depends on the type |
| 99 |
:param object maximum: Maximum value, depends on the type |
| 100 |
|
| 101 |
|
| 102 |
.. code:: python |
| 103 |
|
| 104 |
class AnotherObject(GObject.Object): |
| 105 |
value = 0 |
| 106 |
|
| 107 |
@GObject.Property |
| 108 |
def prop_pyobj(self): |
| 109 |
"""Read only property.""" |
| 110 |
|
| 111 |
return object() |
| 112 |
|
| 113 |
@GObject.Property(type=int) |
| 114 |
def prop_gint(self): |
| 115 |
"""Read-write integer property.""" |
| 116 |
|
| 117 |
return self.value |
| 118 |
|
| 119 |
@prop_gint.setter |
| 120 |
def prop_gint(self, value): |
| 121 |
self.value = value |