I'm struggling to understand layout positioning in Kivy.
My aim is to create a simple form containing 3 rows, each one consisting of a label and a text input.
For example, like on this wireframe drawn with PlantUML, Salt (source):
What I tried
I've tried combining different layouts but cannot achieve the right alignment and I also don't understand what's going wrong.
First attempt:
KV file:
<CustomLabel@Label>:
size_hint: .2, .1
text_size: self.size
<CustomTextField@TextInput>:
size_hint: .2, .1
<CustomFloatLayout@FloatLayout>:
<TestLayout>:
orientation: 'horizontal'
CustomFloatLayout:
CustomLabel:
text: 'Field 1:'
pos_hint: {'x': .1, 'center_y': .95}
CustomTextField:
hint_text: 'hint 1'
pos_hint: {'x': .1, 'center_y': .92}
CustomFloatLayout:
CustomLabel:
text: 'Field 2:'
pos_hint: {'x': .1, 'center_y': .75}
CustomTextField:
hint_text: 'hint 2'
pos_hint: {'x': .1, 'center_y': .72}
CustomFloatLayout:
CustomLabel:
text: 'Field 3:'
pos_hint: {'x': .1, 'center_y': .55}
CustomTextField:
hint_text: 'hint 3'
pos_hint: {'x': .1, 'center_y': .52}
Python code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class TestLayout(BoxLayout):
pass
class TestApp(App):
def build(self):
return TestLayout()
if __name__ == '__main__':
TestApp().run()
Issue
The y-center of the labels is almost at the bottom of the y-center of the text input fields instead of being aligned at the same y-value. And what is even more puzzling: Each row has another offset from the left border of the window.
Second attempt:
KV file:
<CustomLabel@Label>:
size_hint: .2, .1
text_size: self.size
<CustomTextField@TextInput>:
size_hint: .2, .1
<CustomBoxLayout@BoxLayout>:
orientation: 'horizontal'
pos_hint: {'x': .1}
<TestLayout>:
CustomBoxLayout:
CustomLabel:
text: 'Field 1:'
pos_hint: {'x': .1, 'center_y': .95}
CustomTextField:
hint_text: 'hint 1'
pos_hint: {'x': .5, 'center_y': .95}
CustomBoxLayout:
CustomLabel:
text: 'Field 2:'
pos_hint: {'x': .1, 'center_y': .75}
CustomTextField:
hint_text: 'hint 2'
pos_hint: {'x': .5, 'center_y': .75}
CustomBoxLayout:
CustomLabel:
text: 'Field 3:'
pos_hint: {'x': .1, 'center_y': .55}
CustomTextField:
hint_text: 'hint 3'
pos_hint: {'x': .5, 'center_y': .55}
Python code:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class TestLayout(FloatLayout):
pass
class TestApp(App):
def build(self):
return TestLayout()
if __name__ == '__main__':
TestApp().run()
Issue
Looks better, but label and text input are still not aligned at the same center y value. In addition, if I change the pos_hint
of e.g. the second CustomLabel
to pos_hint: {'x': .1, 'bottom': .75}
, then this label appears at the very bottom of the entire window. I would have expected it to be at the bottom of the current BoxLayout row.
Question
- How can I achieve a proper aligning of the labels and the text input fields?
- Why are the labels/input fields of the first attempt located on the diagonal of the window?
- Why do the labels appear at the bottom of the entire window when changing their bottom value within the second attempt code?