Skip to content

Commit df58dc7

Browse files
committed
doc: add a Common Pitfalls section to RelativeLayout
1 parent b81d873 commit df58dc7

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

kivy/uix/relativelayout.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,77 @@
157157
true relative coordinates - relative to a coordinate system with its (0, 0) at
158158
the bottom left corner of the widget in question.
159159
160+
.. _kivy-uix-relativelayout-common-pitfalls:
161+
162+
Common Pitfalls
163+
---------------
164+
165+
As all positions within a :class:`RelativeLayout` are relative to the position
166+
of the layout itself, the position of the layout should never be used in
167+
determining the position of sub-widgets or the layout's :attr:`canvas`.
168+
169+
Take the following kv code for example:
170+
171+
.. container:: align-right
172+
173+
.. figure:: images/relativelayout-fixedposition.png
174+
:scale: 50%
175+
176+
expected result
177+
178+
.. figure:: images/relativelayout-doubleposition.png
179+
:scale: 50%
180+
181+
actual result
182+
183+
.. code::
184+
185+
FloatLayout:
186+
Widget:
187+
size_hint: None, None
188+
size: 200, 200
189+
pos: 200, 200
190+
191+
canvas:
192+
Color:
193+
rgba: 1, 1, 1, 1
194+
Rectangle:
195+
pos: self.pos
196+
size: self.size
197+
198+
RelativeLayout:
199+
size_hint: None, None
200+
size: 200, 200
201+
pos: 200, 200
202+
203+
canvas:
204+
Color:
205+
rgba: 1, 0, 0, 0.5
206+
Rectangle:
207+
pos: self.pos # incorrect
208+
size: self.size
209+
210+
You might expect this to render a single pink rectangle; however, the content
211+
of the :class:`RelativeLayout` is already transformed, so the use of
212+
`pos: self.pos` will double that transformation. In this case, using
213+
`pos: 0, 0` or omitting `pos` completely will provide the expected result.
214+
215+
This also applies to the position of sub-widgets. Instead of positioning a
216+
:class:`~kivy.uix.widget.Widget` based on the layout's own position::
217+
218+
RelativeLayout:
219+
Widget:
220+
pos: self.parent.pos
221+
Widget:
222+
center: self.parent.center
223+
224+
...use the :attr:`pos_hint` property::
225+
226+
RelativeLayout:
227+
Widget:
228+
pos_hint: {'x': 0, 'y': 0}
229+
Widget:
230+
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
160231
161232
.. versionchanged:: 1.7.0
162233
Prior to version 1.7.0, the :class:`RelativeLayout` was implemented as a

kivy/uix/screenmanager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
:class:`~kivy.uix.relativelayout.RelativeLayout`. You need to use that class as
4848
a root widget for your own screen, the best way being to subclass.
4949
50+
.. warning::
51+
As :class:`Screen` is a :class:`~kivy.uix.relativelayout.RelativeLayout`,
52+
it is important to understand the
53+
:ref:`kivy-uix-relativelayout-common-pitfalls`.
54+
5055
Here is an example with a 'Menu Screen' and a 'Settings Screen'::
5156
5257
from kivy.app import App

0 commit comments

Comments
 (0)