| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
Layout Containers |
| 4 |
================= |
| 5 |
|
| 6 |
While many GUI toolkits require you to precisely place widgets in a window, |
| 7 |
using absolute positioning, GTK uses a different approach. |
| 8 |
Rather than specifying the position and size of each widget in the window, |
| 9 |
you can arrange your widgets in rows, columns, and/or tables. |
| 10 |
The size of your window can be determined automatically, based on the sizes |
| 11 |
of the widgets it contains. |
| 12 |
And the sizes of the widgets are, in turn, determined by the amount of text |
| 13 |
they contain, or the minimum and maximum sizes that you specify, and/or how |
| 14 |
you have requested that the available space should be shared between sets of |
| 15 |
widgets. You can perfect your layout by specifying padding distance and |
| 16 |
centering values for each of your widgets. GTK then uses all this information |
| 17 |
to resize and reposition everything sensibly and smoothly when the user |
| 18 |
manipulates the window. |
| 19 |
|
| 20 |
GTK widgets can parent other widgets hierarchically, and the way those widgets |
| 21 |
are displayed depend on the :class:`Gtk.LayoutManager` set in the |
| 22 |
:attr:`Gtk.Widget.props.layout_manager` property. For ease of use, GTK provides |
| 23 |
"pre made" containers that have default layout managers for different needs. |
| 24 |
Most of these containers implement the :class:`Gtk.Orientable` interface, it |
| 25 |
allows to set the widget orientation using |
| 26 |
:attr:`Gtk.Orientable.props.orientation`, you can either set |
| 27 |
:attr:`Gtk.Orientation.HORIZONTAL` or :attr:`Gtk.Orientation.VERTICAL`. |
| 28 |
|
| 29 |
Some containers / layout managers like :class:`Gtk.Box` allow you to influence |
| 30 |
the allocation of a child using the :attr:`Gtk.Widget.props.halign`, |
| 31 |
:attr:`Gtk.Widget.props.valign`, :attr:`Gtk.Widget.props.hexpand` and |
| 32 |
:attr:`Gtk.Widget.props.vexpand` properties. |
| 33 |
|
| 34 |
The main container widgets are: |
| 35 |
|
| 36 |
* :class:`Gtk.Box` |
| 37 |
* :class:`Gtk.CenterBox` |
| 38 |
* :class:`Gtk.HeaderBar` |
| 39 |
* :class:`Gtk.Grid` |
| 40 |
* :class:`Gtk.ListBox` |
| 41 |
* :class:`Gtk.FlowBox` |
| 42 |
* :class:`Gtk.Stack` |
| 43 |
* :class:`Gtk.Notebook` |
| 44 |
|
| 45 |
We are looking into each one in the following sections. |
| 46 |
|
| 47 |
|
| 48 |
Boxes |
| 49 |
----- |
| 50 |
Boxes are invisible containers into which we can pack our widgets. |
| 51 |
When packing widgets into a horizontal box, the objects are inserted |
| 52 |
horizontally from left to right or right to left depending on whether |
| 53 |
:meth:`Gtk.Box.prepend` or :meth:`Gtk.Box.append` is used. |
| 54 |
You can also use the :meth:`Gtk.Box.insert_child_after` or |
| 55 |
:meth:`Gtk.Box.reorder_child_after` methods to insert a widget on a specific |
| 56 |
position. |
| 57 |
In a vertical box, widgets are packed from top to bottom or vice versa. |
| 58 |
You may use any combination of boxes inside or beside other boxes to create |
| 59 |
the desired effect. |
| 60 |
|
| 61 |
Example |
| 62 |
^^^^^^^ |
| 63 |
|
| 64 |
Let's take a look at a slightly modified version of |
| 65 |
the :ref:`extended-example` with two buttons. |
| 66 |
|
| 67 |
.. image:: images/layout_box.png |
| 68 |
|
| 69 |
.. literalinclude:: examples/layout_box.py |
| 70 |
:linenos: |
| 71 |
|
| 72 |
First, we create a horizontally orientated box container where 6 pixels are |
| 73 |
placed between children. This box becomes the child of the top-level window. |
| 74 |
|
| 75 |
.. literalinclude:: examples/layout_box.py |
| 76 |
:lines: 11-12 |
| 77 |
|
| 78 |
Subsequently, we add two different buttons to the box container. |
| 79 |
|
| 80 |
.. literalinclude:: examples/layout_box.py |
| 81 |
:lines: 14-21 |
| 82 |
|
| 83 |
|
| 84 |
CenterBox |
| 85 |
--------- |
| 86 |
|
| 87 |
:class:`Gtk.CenterBox` arranges three children in a row or a column depending on |
| 88 |
its orientation, keeping the middle child centered as well as possible. |
| 89 |
|
| 90 |
To add children you use :meth:`Gtk.CenterBox.set_start_widget`, |
| 91 |
:meth:`Gtk.CenterBox.set_center_widget`, and :meth:`Gtk.CenterBox.set_end_widget`. |
| 92 |
|
| 93 |
Example |
| 94 |
^^^^^^^ |
| 95 |
|
| 96 |
.. image:: images/layout_center.png |
| 97 |
|
| 98 |
.. literalinclude:: examples/layout_center.py |
| 99 |
:linenos: |
| 100 |
|
| 101 |
|
| 102 |
HeaderBar |
| 103 |
--------- |
| 104 |
A :class:`Gtk.HeaderBar` is similar to a horizontal :class:`Gtk.CenterBox`, it |
| 105 |
allows to place children at the start or the end. In addition, it allows a title |
| 106 |
to be displayed. The title will be centered with respect to the width of the box, |
| 107 |
even if the children at either side take up different amounts of space. |
| 108 |
|
| 109 |
:class:`Gtk.HeaderBar` is designed to be used as a window titlebar. |
| 110 |
This means that it can show typical window frame controls, such as minimize, |
| 111 |
maximize and close buttons, or the window icon. |
| 112 |
It is also draggable, meaning that you can move the parent window from it. |
| 113 |
You can use the :meth:`Gtk.Window.set_titlebar` method to set it as so. |
| 114 |
|
| 115 |
To add children you use :meth:`Gtk.HeaderBar.pack_start`, |
| 116 |
:meth:`Gtk.HeaderBar.pack_end`, and :meth:`Gtk.HeaderBar.set_title_widget` for |
| 117 |
the center one. By default if no ``title_widget`` is set |
| 118 |
it will have a label with the window's :attr:`Gtk.Window.props.title`. |
| 119 |
|
| 120 |
Example |
| 121 |
^^^^^^^ |
| 122 |
|
| 123 |
.. image:: images/layout_headerbar.png |
| 124 |
|
| 125 |
.. literalinclude:: examples/layout_headerbar.py |
| 126 |
:linenos: |
| 127 |
|
| 128 |
|
| 129 |
Grid |
| 130 |
---- |
| 131 |
:class:`Gtk.Grid` is a container which arranges its child widgets in rows and |
| 132 |
columns, but you do not need to specify the dimensions in the constructor. |
| 133 |
Children are added using :meth:`Gtk.Grid.attach`. They can span multiple rows or |
| 134 |
columns. The :meth:`Gtk.Grid.attach` method takes five parameters: |
| 135 |
|
| 136 |
1. The ``child`` parameter is the :class:`Gtk.Widget` to add. |
| 137 |
2. ``left`` is the column number to attach the left side of ``child`` to. |
| 138 |
3. ``top`` indicates the row number to attach the top side of ``child`` to. |
| 139 |
4. ``width`` and ``height`` indicate the number of columns that the ``child`` |
| 140 |
will span, and the number of rows that the ``child`` will span, respectively. |
| 141 |
|
| 142 |
It is also possible to add a child next to an existing child, using |
| 143 |
:meth:`Gtk.Grid.attach_next_to`, which also takes five parameters: |
| 144 |
|
| 145 |
1. ``child`` is the :class:`Gtk.Widget` to add, as above. |
| 146 |
2. ``sibling`` is an existing child widget of ``self`` (a :class:`Gtk.Grid` instance) |
| 147 |
or ``None``. The ``child`` widget will be placed next to ``sibling``, or |
| 148 |
if ``sibling`` is ``None``, at the beginning or end of the grid. |
| 149 |
3. ``side`` is a :class:`Gtk.PositionType` indicating the side of |
| 150 |
``sibling`` that ``child`` is positioned next to. |
| 151 |
4. ``width`` and ``height`` indicate the number of columns and rows |
| 152 |
the ``child`` widget will span, respectively. |
| 153 |
|
| 154 |
Example |
| 155 |
^^^^^^^ |
| 156 |
|
| 157 |
.. image:: images/layout_grid.png |
| 158 |
|
| 159 |
.. literalinclude:: examples/layout_grid.py |
| 160 |
:linenos: |
| 161 |
|
| 162 |
|
| 163 |
ListBox |
| 164 |
------- |
| 165 |
|
| 166 |
A :class:`Gtk.ListBox` is a vertical container that contains :class:`Gtk.ListBoxRow` |
| 167 |
children. These rows can be dynamically sorted and filtered, and headers can be |
| 168 |
added dynamically depending on the row content. It also allows keyboard and |
| 169 |
mouse navigation and selection like a typical list. |
| 170 |
|
| 171 |
Although a :class:`Gtk.ListBox` must have only :class:`Gtk.ListBoxRow` children, |
| 172 |
you can add any kind of widget to it via :meth:`Gtk.ListBox.append` or |
| 173 |
:meth:`Gtk.ListBox.insert` and a :class:`Gtk.ListBoxRow` widget will |
| 174 |
automatically be inserted between the list and the widget. |
| 175 |
|
| 176 |
:class:`Gtk.ListBox` allows activating and selecting its children. |
| 177 |
Selection can be configured with :attr:`Gtk.ListBox.props.selection_mode`, |
| 178 |
selection modes are :attr:`Gtk.SelectionMode.NONE` (not selection at all), |
| 179 |
:attr:`Gtk.SelectionMode.SINGLE` (one or none elements can be selected), |
| 180 |
:attr:`Gtk.SelectionMode.BROWSE` (user can't deselect a currently selected |
| 181 |
element except by selecting another element) and |
| 182 |
:attr:`Gtk.SelectionMode.MULTIPLE` (any number of elements may be selected). |
| 183 |
|
| 184 |
It will emit the :func:`row-activated <Gtk.ListBox.signals.row_activated>` |
| 185 |
signal when a row is activated in any form, and |
| 186 |
:func:`row-selected <Gtk.ListBox.signals.row_selected>` when a row is selected. |
| 187 |
|
| 188 |
Example |
| 189 |
^^^^^^^ |
| 190 |
|
| 191 |
.. image:: images/layout_listbox.png |
| 192 |
|
| 193 |
.. literalinclude:: examples/layout_listbox.py |
| 194 |
:linenos: |
| 195 |
|
| 196 |
|
| 197 |
FlowBox |
| 198 |
------- |
| 199 |
|
| 200 |
A :class:`Gtk.FlowBox` is a container that positions child widgets in sequence |
| 201 |
according to its orientation. |
| 202 |
|
| 203 |
For instance, with the horizontal orientation, the widgets will be arranged |
| 204 |
from left to right, starting a new row under the previous row when necessary. |
| 205 |
Reducing the width in this case will require more rows, so a larger height |
| 206 |
will be requested. |
| 207 |
|
| 208 |
Likewise, with the vertical orientation, the widgets will be arranged from top |
| 209 |
to bottom, starting a new column to the right when necessary. Reducing the |
| 210 |
height will require more columns, so a larger width will be requested. |
| 211 |
|
| 212 |
:class:`Gtk.FlowBox` behaves similar to :class:`Gtk.ListBox`, we could say that |
| 213 |
is its "grid" counterpart. Just like :class:`Gtk.ListBox`, the children of a |
| 214 |
:class:`Gtk.FlowBox` can be dynamically sorted and filtered, and also activated |
| 215 |
and selected. |
| 216 |
|
| 217 |
Although a :class:`Gtk.FlowBox` must have only :class:`Gtk.FlowBoxChild` |
| 218 |
children, you can add any kind of widget to it via |
| 219 |
:meth:`Gtk.FlowBox.append` or :meth:`Gtk.FlowBox.insert`, and a |
| 220 |
:class:`Gtk.FlowBoxChild` widget will automatically be inserted between the box |
| 221 |
and the widget. |
| 222 |
|
| 223 |
Example |
| 224 |
^^^^^^^ |
| 225 |
|
| 226 |
.. image:: images/layout_flowbox.png |
| 227 |
|
| 228 |
.. literalinclude:: examples/layout_flowbox.py |
| 229 |
:linenos: |
| 230 |
|
| 231 |
Stack and StackSwitcher |
| 232 |
----------------------- |
| 233 |
|
| 234 |
A :class:`Gtk.Stack` is a container which only shows one of its children at a |
| 235 |
time. In contrast to :class:`Gtk.Notebook`, :class:`Gtk.Stack` does not provide |
| 236 |
a means for users to change the visible child. Instead, the |
| 237 |
:class:`Gtk.StackSwitcher` widget can be used with :class:`Gtk.Stack` to |
| 238 |
provide this functionality. |
| 239 |
|
| 240 |
Transitions between pages can be animated as slides or fades. This can be |
| 241 |
controlled with :attr:`Gtk.Stack.props.transition_type`. These animations |
| 242 |
respect the ``gtk-enable-animations`` setting. |
| 243 |
|
| 244 |
Transition speed can be adjusted with :attr:`Gtk.Stack.props.transition_duration`. |
| 245 |
|
| 246 |
The :class:`Gtk.StackSwitcher` widget acts as a controller for a |
| 247 |
:class:`Gtk.Stack`; it shows a row of buttons to switch between the various |
| 248 |
pages of the associated stack widget. |
| 249 |
|
| 250 |
:class:`Gtk.Stack` uses the auxiliary :class:`Gtk.StackPage` object. |
| 251 |
:class:`Gtk.Stack` will return a :class:`Gtk.StackPage` every time you add a new |
| 252 |
children, either with :meth:`Gtk.Stack.add_child`, :meth:`Gtk.Stack.add_named` |
| 253 |
or :meth:`Gtk.Stack.add_titled`. |
| 254 |
:class:`Gtk.StackPage` holds important properties for the stack's children, like |
| 255 |
the name, the title to display, or if the page needs attention, then this data |
| 256 |
can be used for example by :class:`Gtk.StackSwitcher`. |
| 257 |
|
| 258 |
It is possible to associate multiple :class:`Gtk.StackSwitcher` widgets with |
| 259 |
the same :class:`Gtk.Stack` widget. |
| 260 |
|
| 261 |
Example |
| 262 |
^^^^^^^ |
| 263 |
|
| 264 |
.. image:: images/layout_stack.png |
| 265 |
|
| 266 |
.. literalinclude:: examples/layout_stack.py |
| 267 |
:linenos: |
| 268 |
|
| 269 |
Notebook |
| 270 |
-------- |
| 271 |
|
| 272 |
The :class:`Gtk.Notebook` is a container whose children are pages switched |
| 273 |
between using tabs. |
| 274 |
|
| 275 |
There are many configuration options for GtkNotebook. Among other things, you |
| 276 |
can choose on which edge the tabs appear (see :meth:`Gtk.Notebook.set_tab_pos`), |
| 277 |
whether, if there are too many tabs to fit the notebook should be made bigger or |
| 278 |
scrolling arrows added (see :meth:`Gtk.Notebook.set_scrollable`), and whether |
| 279 |
there will be a popup menu allowing the users to switch pages (see |
| 280 |
:meth:`Gtk.Notebook.popup_enable`, :meth:`Gtk.Notebook.popup_disable`). |
| 281 |
|
| 282 |
You can add children with :meth:`Gtk.Notebook.append_page`, it takes two widgets, |
| 283 |
the first is the widget to show as a page, and the second is the widget to show |
| 284 |
as the tab content. |
| 285 |
|
| 286 |
Example |
| 287 |
^^^^^^^ |
| 288 |
|
| 289 |
.. image:: images/layout_notebook.png |
| 290 |
|
| 291 |
.. literalinclude:: examples/layout_notebook.py |
| 292 |
:linenos: |