| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
============= |
| 4 |
Flags & Enums |
| 5 |
============= |
| 6 |
|
| 7 |
Flags are subclasses of :class:`GObject.GFlags`, which in turn is a |
| 8 |
subclass of the standard library :class:`enum.IntFlag`. They represent |
| 9 |
bit fields where some bits also have names: |
| 10 |
|
| 11 |
.. code:: pycon |
| 12 |
|
| 13 |
>>> Gtk.DialogFlags.MODAL |
| 14 |
<DialogFlags.MODAL: 1> |
| 15 |
>>> Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT |
| 16 |
<DialogFlags.MODAL|DESTROY_WITH_PARENT: 3> |
| 17 |
>>> int(_) |
| 18 |
3 |
| 19 |
>>> Gtk.DialogFlags(3) |
| 20 |
<DialogFlags.MODAL|DESTROY_WITH_PARENT: 3> |
| 21 |
>>> isinstance(Gtk.DialogFlags.MODAL, Gtk.DialogFlags) |
| 22 |
True |
| 23 |
>>> |
| 24 |
|
| 25 |
Bitwise operations on them will produce a value of the same type. |
| 26 |
|
| 27 |
|
| 28 |
Enums are subclasses of :class:`GObject.GEnum`, which in turn is a |
| 29 |
subclass of the standard library :class:`enum.IntEnum`. They represent |
| 30 |
a list of named constants: |
| 31 |
|
| 32 |
.. code:: pycon |
| 33 |
|
| 34 |
>>> Gtk.Align.CENTER |
| 35 |
<Align.CENTER: 3> |
| 36 |
>>> int(Gtk.Align.CENTER) |
| 37 |
3 |
| 38 |
>>> int(Gtk.Align.END) |
| 39 |
2 |
| 40 |
>>> Gtk.Align(1) |
| 41 |
<Align.START: 1> |
| 42 |
>>> isinstance(Gtk.Align.CENTER, Gtk.Align) |
| 43 |
True |
| 44 |
|
| 45 |
Creating New Enums and Flags |
| 46 |
---------------------------- |
| 47 |
|
| 48 |
New enumerations and flags types can be defined by subclassing |
| 49 |
:class:`GObject.GEnum` or :class:`GObject.GFlags` in the same way as |
| 50 |
standard library enumerations. A new GType is registered |
| 51 |
automatically. |
| 52 |
|
| 53 |
.. code:: pycon |
| 54 |
|
| 55 |
>>> from gi.repository import GObject |
| 56 |
>>> class E(GObject.GEnum): |
| 57 |
... ONE = 1 |
| 58 |
... TWO = 2 |
| 59 |
... |
| 60 |
>>> E.ONE |
| 61 |
<E.ONE: 1> |
| 62 |
>>> E.ONE.value_name |
| 63 |
'ONE' |
| 64 |
>>> E.ONE.value_nick |
| 65 |
'one' |
| 66 |
>>> E.__gtype__ |
| 67 |
<GType __main__+E (1014834640)> |
| 68 |
>>> E.__gtype__.name |
| 69 |
'__main__+E' |
| 70 |
|
| 71 |
The GType name can be set explicitly by providing a ``__gtype_name__`` |
| 72 |
attribute: |
| 73 |
|
| 74 |
.. code:: pycon |
| 75 |
|
| 76 |
>>> from gi.repository import GObject |
| 77 |
>>> class MyEnum(GObject.GEnum): |
| 78 |
... __gtype_name__ = "MyEnum" |
| 79 |
... ONE = 1 |
| 80 |
... |
| 81 |
>>> MyEnum.__gtype__ |
| 82 |
<GType MyEnum (767309744)> |