|
157 | 157 | true relative coordinates - relative to a coordinate system with its (0, 0) at
|
158 | 158 | the bottom left corner of the widget in question.
|
159 | 159 |
|
| 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} |
160 | 231 |
|
161 | 232 | .. versionchanged:: 1.7.0
|
162 | 233 | Prior to version 1.7.0, the :class:`RelativeLayout` was implemented as a
|
|
0 commit comments