Version

Quick search

Layout

Layouts are used to calculate and assign widget positions.

The Layout class itself cannot be used directly. You should use one of the following layout classes:

Understanding the size_hint Property in Widget

The size_hint is a tuple of values used by layouts to manage the sizes of their children. It indicates the size relative to the layout’s size instead of an absolute size (in pixels/points/cm/etc). The format is:

widget.size_hint = (width_percent, height_percent)

The percent is specified as a floating point number in the range 0-1. For example, 0.5 is 50%, 1 is 100%.

If you want a widget’s width to be half of the parent’s width and the height to be identical to the parent’s height, you would do:

widget.size_hint = (0.5, 1.0)

If you don’t want to use a size_hint for either the width or height, set the value to None. For example, to make a widget that is 250px wide and 30% of the parent’s height, do:

widget.size_hint = (None, 0.3)
widget.width = 250

Being Kivy properties, these can also be set via constructor arguments:

widget = Widget(size_hint=(None, 0.3), width=250)

Changed in version 1.4.1: The reposition_child internal method (made public by mistake) has been removed.

class kivy.uix.layout.Layout(**kwargs)[source]

Bases: kivy.uix.widget.Widget

Layout interface class, used to implement every layout. See module documentation for more information.

add_widget(widget, index=0, canvas=None)[source]

Add a new widget as a child of this widget.

Parameters
widget: Widget

Widget to add to our list of children.

index: int, defaults to 0

Index to insert the widget in the list. Notice that the default of 0 means the widget is inserted at the beginning of the list and will thus be drawn on top of other sibling widgets. For a full discussion of the index and widget hierarchy, please see the Widgets Programming Guide.

New in version 1.0.5.

canvas: str, defaults to None

Canvas to add widget’s canvas to. Can be ‘before’, ‘after’ or None for the default canvas.

New in version 1.9.0.

>>> from kivy.uix.button import Button
>>> from kivy.uix.slider import Slider
>>> root = Widget()
>>> root.add_widget(Button())
>>> slider = Slider()
>>> root.add_widget(slider)
do_layout(*largs)[source]

This function is called when a layout is called by a trigger. If you are writing a new Layout subclass, don’t call this function directly but use _trigger_layout() instead.

The function is by default called before the next frame, therefore the layout isn’t updated immediately. Anything depending on the positions of e.g. children should be scheduled for the next frame.

New in version 1.0.8.

layout_hint_with_bounds(sh_sum, available_space, min_bounded_size, sh_min_vals, sh_max_vals, hint)[source]

(internal) Computes the appropriate (size) hint for all the widgets given (potential) min or max bounds on the widgets’ size. The hint list is updated with appropriate sizes.

It walks through the hints and for any widgets whose hint will result in violating min or max constraints, it fixes the hint. Any remaining or missing space after all the widgets are fixed get distributed to the widgets making them smaller or larger according to their size hint.

This algorithms knows nothing about the widgets other than what is passed through the input params, so it’s fairly generic for laying things out according to constraints using size hints.

Parameters
sh_sum: float

The sum of the size hints (basically sum(size_hint)).

available_space: float

The amount of pixels available for all the widgets whose size hint is not None. Cannot be zero.

min_bounded_size: float

The minimum amount of space required according to the size_hint_min of the widgets (basically sum(size_hint_min)).

sh_min_vals: list or iterable

Items in the iterable are the size_hint_min for each widget. Can be None. The length should be the same as hint

sh_max_vals: list or iterable

Items in the iterable are the size_hint_max for each widget. Can be None. The length should be the same as hint

hint: list

A list whose size is the same as the length of sh_min_vals and sh_min_vals whose each element is the corresponding size hint value of that element. This list is updated in place with correct size hints that ensure the constraints are not violated.

Returns

Nothing. hint is updated in place.

remove_widget(widget)[source]

Remove a widget from the children of this widget.

Parameters
widget: Widget

Widget to remove from our children list.

>>> from kivy.uix.button import Button
>>> root = Widget()
>>> button = Button()
>>> root.add_widget(button)
>>> root.remove_widget(button)