Debian Codegraph @

docs/tutorials/gobject/basics.rst

raw
1 .. currentmodule:: gi.repository
2
3 Basics
4 ======
5
6 .. hint::
7 In this example, we will use GTK widgets to demonstrate GObject
8 capabilities.
9
10 GObject Initialization
11 ----------------------
12
13 GObjects are initialized like any other Python class.
14
15 .. code:: python
16
17 label = Gtk.Label()
18
19 .. _basics-properties:
20
21 Properties
22 ----------
23
24 GObject has a powerful properties system.
25
26 Properties describe the configuration and state of a gobject. Each gobject has
27 its own particular set of properties. For example, a GTK button has the property
28 ``label`` which contains the text of the label widget inside the button.
29
30 You can specify the name and value of any number of properties as keyword
31 arguments when creating an instance of a gobject. To create a label aligned to
32 the right with the text "Hello World", use:
33
34 .. code:: python
35
36 label = Gtk.Label(label='Hello World', halign=Gtk.Align.END)
37
38 which is equivalent to
39
40 .. code:: python
41
42 label = Gtk.Label()
43 label.set_label('Hello World')
44 label.set_halign(Gtk.Align.END)
45
46 There are various ways of interacting with a gobject properties from Python, we
47 already have seen the two first ways, these are setting them on initialization
48 or using the getters and setters functions that the gobject might provide.
49
50 Other option is to use :class:`GObject.Object` builtin methods :meth:`GObject.Object.get_property`
51 and :meth:`GObject.Object.set_property`. Using these methods is more common when
52 you have created a :class:`GObject.Object` subclass and you don't have getters
53 and setters functions.
54
55 .. code:: python
56
57 label = Gtk.Label()
58 label.set_property('label', 'Hello World')
59 label.set_property('halign', Gtk.Align.END)
60 print(label.get_property('label'))
61
62 Instead of using getters and setters you can also get and set the gobject
63 properties through the ``props`` property such as ``label.props.label = 'Hello World'``.
64 This is equivalent to the more verbose methods that we saw before, and it's a
65 more pythonic way of interacting with properties.
66
67 To see which properties are available for a gobject you can ``dir`` the
68 ``props`` property:
69
70 .. code:: python
71
72 widget = Gtk.Box()
73 print(dir(widget.props))
74
75 This will print to the console the list of properties that a :class:`Gtk.Box`
76 has.
77
78 Property Bindings
79 ^^^^^^^^^^^^^^^^^
80
81 GObject provides a practical way to bind properties of two different gobjects.
82 This is done using the :meth:`GObject.Object.bind_property` method.
83
84 The behavior of this binding can be controlled by passing a
85 :class:`GObject.BindingFlags` of choice.
86 :attr:`GObject.BindingFlags.DEFAULT` will update the target property every time
87 the source property changes.
88 :attr:`GObject.BindingFlags.BIDIRECTIONAL` creates a bidirectional binding; if
89 either the property of the source or the property of the target changes, the
90 other is updated.
91 :attr:`GObject.BindingFlags.SYNC_CREATE` is similar to ``DEFAULT`` but it will
92 also synchronize the values of the source and target properties when creating
93 the binding.
94 :attr:`GObject.BindingFlags.INVERT_BOOLEAN` works only for boolean properties
95 and setting one property to ``True`` will result in the other being set to
96 ``False`` and vice versa (this flag cannot be used when passing custom
97 transformation functions to :meth:`GObject.Object.bind_property`).
98
99 .. code:: python
100
101 entry = Gtk.Entry()
102 label = Gtk.Label()
103
104 entry.bind_property('text', label, 'label', GObject.BindingFlags.DEFAULT)
105
106 In this example **entry** is our source object and ``text`` the source property
107 to bind.
108 **label** is the target object and the namesake property ``label`` is the
109 target property.
110 Every time someone changes the ``text`` property of the entry the label
111 ``label`` will be updated as well with the same value.
112
113 Property Bindings with Transformations
114 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115
116 Sometimes you may want to bind two properties that are incompatible, or you
117 simply need to apply some transformation between these values.
118 For these scenarios :meth:`GObject.Object.bind_property` also accepts custom
119 transformation functions that serve to this purpose.
120
121 The transformation functions take as first argument the :class:`GObject.Binding`
122 instance for this binding and as second argument the property value depending
123 on the direction of the transformation.
124 For ``transform_to`` this will be the value of the source property and for
125 ``transform_from`` the value of the target property.
126 Each function should return the value to be set in the other object's property,
127 with the correct type.
128
129 In this example we'll do an ``int`` to ``bool`` type conversion between two
130 objects:
131
132 .. code:: python
133
134 def transform_to(_binding, value):
135 return bool(value) # Return int converted to a bool
136
137 def transform_from(_binding, value):
138 return int(value) # Return bool converted to a int
139
140 source.bind_property(
141 'int_prop',
142 target,
143 'bool_prop',
144 GObject.BindingFlags.BIDIRECTIONAL,
145 transform_to,
146 transform_from
147 )
148
149 .. _basics-signals:
150
151 Signals
152 -------
153
154 GObject signals are a system for registering callbacks for specific events.
155
156 A generic example is:
157
158 .. code:: python
159
160 handler_id = gobject.connect('event', callback, data)
161
162 Firstly, *gobject* is an instance of a gobject we created earlier. Next, the
163 event we are interested in. Each gobject has its own particular events which
164 can occur.
165 This means that when the gobject emits the event, the signal is issued.
166 Thirdly, the *callback* argument is the name of the callback function.
167 It contains the code which runs when signals of the specified type are issued.
168 Finally, the *data* argument includes any data which should be passed when the
169 signal is issued. However, this argument is completely optional and can be left
170 out if not required.
171
172 The function returns a number that identifies this particular signal-callback
173 pair.
174 It is required to disconnect from a signal such that the callback function will
175 not be called during any future or currently ongoing emissions of the signal it
176 has been connected to:
177
178 .. code:: python
179
180 gobject.disconnect(handler_id)
181
182 When creating the callback function for a signal, the arguments it accepts will
183 depend on the specific signal, but for a signal with no arguments it will look
184 like this:
185
186 .. code:: python
187
188 def on_event(gobject, data):
189 ...
190
191 my_object.connect('event', on_event, data)
192
193 Where ``gobject`` is the object that triggered the signal and ``data`` is the
194 additional data that we previously passed to the :meth:`GObject.Object.connect`
195 method.
196 If the signal had arguments, they will come before the optional data argument.
197
198 The ``notify`` signal
199 ^^^^^^^^^^^^^^^^^^^^^
200
201 When any of a GObject's properties change, it will emit the ``notify`` signal.
202 This is a "detailed" signal, meaning that you can listen to a subset of the
203 signal, in this case a specific property.
204 For example, you can connect to the signal in the form of
205 ``notify::property-name``:
206
207 .. code:: python
208
209 def callback(label, _pspec):
210 print(f'The label prop changed to {label.props.label}')
211
212 label = Gtk.Label()
213 label.connect('notify::label', callback)
214