/
objects.txt
395 lines (265 loc) · 13.4 KB
/
objects.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
.. _objects:
Objects
=======
GObject is the fundamental type providing the common attributes and methods
for all object types in GTK+, Pango and other libraries based on GObject.
The :class:`GObject.GObject` class provides methods for object construction and destruction,
property access methods, and signal support.
This section will introduce some important aspects about the GObject
implementation in python.
Inherit from GObject.GObject
----------------------------
A native GObject is accessible via :class:`GObject.GObject`. It is rarely instantiated
directly, we generally use inherited class. A :class:`Gtk.Widget` is an inherited class
of a :class:`GObject.GObject`. It may be interesting to make an inherited class to create
a new widget, like a settings dialog.
To inherit from :class:`GObject.GObject`, you must call :meth:`GObject.GObject.__init__`
in your constructor (if the class inherits from :class:`Gtk.Button`, it must
call :func:`Gtk.Button.__init__` for instance), like in the example below:
.. code-block:: python
from gi.repository import GObject
class MyObject(GObject.GObject):
def __init__(self):
GObject.GObject.__init__(self)
Signals
-------
Signals connect arbitrary application-specific events with any number of listeners.
For example, in GTK+, every user event (keystroke or mouse move) is received
from the X server and generates a GTK+ event under the form of a signal emission
on a given object instance.
Each signal is registered in the type system together with the type on which it
can be emitted: users of the type are said to connect to the signal on a given
type instance when they register a function to be invoked upon the signal
emission. Users can also emit the signal by themselves or stop the emission of
the signal from within one of the functions connected to the signal.
Receive signals
^^^^^^^^^^^^^^^
See :ref:`signals`
Create new signals
^^^^^^^^^^^^^^^^^^
New signals can be created by adding them to
:attr:`GObject.GObject.__gsignals__`, a dictionary:
When a new signal is created, a method handler can also be defined, it will be
called each time the signal is emitted. It is called do_signal_name.
.. code-block:: python
class MyObject(GObject.GObject):
__gsignals__ = {
'my_signal': (GObject.SIGNAL_RUN_FIRST, None,
(int,))
}
def do_my_signal(self, arg):
print("class method for `my_signal' called with argument", arg)
:const:`GObject.SIGNAL_RUN_FIRST` indicates that this signal will invoke the object method
handler (:meth:`do_my_signal` here) in the first emission stage. Alternatives are
:const:`GObject.SIGNAL_RUN_LAST` (the method handler will be invoked in the third
emission stage) and :const:`GObject.SIGNAL_RUN_CLEANUP` (invoke the method handler in the
last emission stage).
The second part, ``None``, indicates the return type of the signal,
usually ``None``.
``(int,)`` indicates the signal arguments, here, the signal will only
take one argument, whose type is int. This argument type list must end with a
comma.
Signals can be emitted using :meth:`GObject.GObject.emit`:
.. code-block:: python
my_obj.emit("my_signal", 42) # emit the signal "my_signal", with the
# argument 42
Properties
----------
One of GObject's nice features is its generic get/set mechanism for object
properties. Each class inherited from :class:`GObject.GObject` can define new
properties. Each property has a type which never changes (e.g. str, float,
int...). For instance, they are used for :class:`Gtk.Button` where there is a
"label" property which contains the text of the button.
Use existing properties
^^^^^^^^^^^^^^^^^^^^^^^
The class :class:`GObject.GObject` provides several useful functions to manage
existing properties, :func:`GObject.GObject.get_property` and :func:`GObject.GObject.set_property`.
Some properties also have functions dedicated to them, called getter and setter.
For the property "label" of a button, there are two functions to get and set
them, :func:`Gtk.Button.get_label` and :func:`Gtk.Button.set_label`.
Create new properties
^^^^^^^^^^^^^^^^^^^^^
A property is defined with a name and a type. Even if python itself is
dynamically typed, you can't change the type of a property once it is defined. A
property can be created using :class:`GObject.Property`.
.. code-block:: python
from gi.repository import GObject
class MyObject(GObject.GObject):
foo = GObject.Property(type=str, default='bar')
property_float = GObject.Property(type=float)
def __init__(self):
GObject.GObject.__init__(self)
Properties can also be read-only, if you want some properties to be readable but
not writable. To do so, you can add some flags to the property definition, to control read/write access.
Flags are :const:`GObject.PARAM_READABLE` (only read access for external code),
:const:`GObject.PARAM_WRITABLE` (only write access),
:const:`GObject.PARAM_READWRITE` (public):
.. there is also construct things, but they
.. doesn't seem to be functional in python
.. code-block:: python
foo = GObject.Property(type=str, flags = GObject.PARAM_READABLE) # not writable
bar = GObject.Property(type=str, flags = GObject.PARAM_WRITABLE) # not readable
You can also define new read-only properties with a new method decorated with
:class:`GObject.Property`:
.. code-block:: python
from gi.repository import GObject
class MyObject(GObject.GObject):
def __init__(self):
GObject.GObject.__init__(self)
@GObject.Property
def readonly(self):
return 'This is read-only.'
You can get this property using:
.. code-block:: python
my_object = MyObject()
print(my_object.readonly)
print(my_object.get_property("readonly"))
The API of :class:`GObject.Property` is similar to the builtin :py:func:`property`. You can create property setter in a way similar to Python property:
.. code-block:: python
class AnotherObject(GObject.Object):
value = 0
@GObject.Property
def prop(self):
'Read only property.'
return 1
@GObject.Property(type=int)
def propInt(self):
'Read-write integer property.'
return self.value
@propInt.setter
def propInt(self, value):
self.value = value
There is also a way to define minimum and maximum values for numbers, using a more verbose form:
.. code-block:: python
from gi.repository import GObject
class MyObject(GObject.GObject):
__gproperties__ = {
"int-prop": (int, # type
"integer prop", # nick
"A property that contains an integer", # blurb
1, # min
5, # max
2, # default
GObject.PARAM_READWRITE # flags
),
}
def __init__(self):
GObject.GObject.__init__(self)
self.int_prop = 2
def do_get_property(self, prop):
if prop.name == 'int-prop':
return self.int_prop
else:
raise AttributeError, 'unknown property %s' % prop.name
def do_set_property(self, prop, value):
if prop.name == 'int-prop':
self.int_prop = value
else:
raise AttributeError, 'unknown property %s' % prop.name
Properties must be defined in :attr:`GObject.GObject.__gproperties__`, a
dictionary, and handled in do_get_property and do_set_property.
Watch properties
^^^^^^^^^^^^^^^^
When a property is modified, a signal is emitted, whose name is
"notify::property_name":
.. code-block:: python
my_object = MyObject()
def on_notify_foo(obj, gparamstring):
print("foo changed")
my_object.connect("notify::foo", on_notify_foo)
my_object.set_property("foo", "bar") # on_notify_foo will be called
API
---
.. class:: GObject.GObject
.. method:: get_property(property_name)
Retrieves a property value.
.. method:: set_property(property_name, value)
Set property *property_name* to *value*.
.. method:: emit(signal_name, ...)
Emit signal *signal_name*. Signal arguments must follow, e.g. if your
signal is of type ``(int,)``, it must be emitted with::
self.emit(signal_name, 42)
.. method:: freeze_notify()
This method freezes all the "notify::" signals (which are
emitted when any property is changed) until the :meth:`thaw_notify` method is
called.
It recommended to use the *with* statement when calling :meth:`freeze_notify`,
that way it is ensured that :meth:`thaw_notify` is called implicitly at
the end of the block::
with an_object.freeze_notify():
# Do your work here
...
.. method:: thaw_notify()
Thaw all the "notify::" signals which were thawed by :meth:`freeze_notify`.
It is recommended to not call :meth:`thaw_notify` explicitly but use
:meth:`freeze_notify` together with the *with* statement.
.. method:: handler_block(handler_id)
Blocks a handler of an instance so it will not be called during any signal
emissions unless :meth:`handler_unblock` is called for that *handler_id*.
Thus "blocking" a signal handler means to temporarily deactivate it, a
signal handler has to be unblocked exactly the same amount of times it
has been blocked before to become active again.
It is recommended to use :meth:`handler_block` in conjunction with the
*with* statement which will call :meth:`handler_unblock` implicitly at
the end of the block::
with an_object.handler_block(handler_id):
# Do your work here
...
.. method:: handler_unblock(handler_id)
Undoes the effect of :meth:`handler_block`. A blocked handler is skipped
during signal emissions and will not be invoked until it has been unblocked
exactly the amount of times it has been blocked before.
It is recommended to not call :meth:`handler_unblock` explicitly but use
:meth:`handler_block` together with the *with* statement.
.. attribute:: __gsignals__
A dictionary where inherited class can define new signals.
Each element in the dictionary is a new signal. The key is the signal
name. The value is a tuple, with the form::
(GObject.SIGNAL_RUN_FIRST, None, (int,))
:const:`GObject.SIGNAL_RUN_FIRST` can be replaced with
:const:`GObject.SIGNAL_RUN_LAST` or :const:`GObject.SIGNAL_RUN_CLEANUP`.
``None`` is the return type of the signal. ``(int,)`` is the list of the
parameters of the signal, it must end with a comma.
.. attribute:: __gproperties__
.. based on http://www.pygtk.org/articles/subclassing-gobject/sub-classing-gobject-in-python.htm
The :attr:`__gproperties__` dictionary is a class property where you define the
properties of your object. This is not the recommend way to define new
properties, the method written above is much less verbose. The benefits
of this method is that a property can be defined with more settings,
like the minimum or the maximum for numbers.
The key is the name of the property
The value is a tuple which describe the property.
The number of elements of this tuple depends on its first element but
the tuple will always contain at least the following items:
The first element is the property's type (e.g. ``int``, ``float``...).
The second element is the property's nick name, which is a string
with a short description of the property. This is generally used by
programs with strong introspection capabilities, like the graphical
user interface builder `Glade`_.
The third one is the property's description or blurb, which is
another string with a longer description of the property. Also used
by `Glade`_ and similar programs.
The last one (which is not necessarily the forth one as we will see
later) is the property's flags: :const:`GObject.PARAM_READABLE`,
:const:`GObject.PARAM_WRITABLE`, :const:`GObject.PARAM_READWRITE`.
The absolute length of the tuple depends on the property type (the
first element of the tuple). Thus we have the following situations:
If the type is ``bool`` or ``str``, the forth element is the
default value of the property.
If the type is ``int`` or ``float``, the forth element is the minimum
accepted value, the fifth element is the maximum accepted value
and the sixth element is the default value.
If the type is not one of these, there is no extra element.
.. attribute:: GObject.SIGNAL_RUN_FIRST
Invoke the object method handler in the first emission stage.
.. attribute:: GObject.SIGNAL_RUN_LAST
Invoke the object method handler in the third emission stage.
.. attribute:: GObject.SIGNAL_RUN_CLEANUP
Invoke the object method handler in the last emission stage.
.. attribute:: GObject.PARAM_READABLE
The property is readable.
.. attribute:: GObject.PARAM_WRITABLE
The property is writable.
.. attribute:: GObject.PARAM_READWRITE
The property is readable and writable.
.. _Glade: http://glade.gnome.org/