14

I'm using the following Kivy code to create BoxLayout with buttons:

BoxLayout:
    orientation: "vertical"
    width: 200
    size_hint_x: None

    Button:
        size_hint_y: None
        height: 30
        text: 'btn1'

    Button:
        size_hint_y: None
        height: 30
        text: 'btn2'

    Button:
        size_hint_y: None
        height: 30
        text: 'btn3'

But buttons stick to the bottom edge, how can i push them towards the top edge of the layout?

enter image description here

DikobrAz
  • 3,557
  • 4
  • 35
  • 53

3 Answers3

25

You can also put an empty Widget at the end to take up the space.

BoxLayout:
    orientation: "vertical"
    width: 200
    size_hint_x: None

    Button:
        size_hint_y: None
        height: 30
        text: 'btn1'

    Button:
        size_hint_y: None
        height: 30
        text: 'btn2'

    Button:
        size_hint_y: None
        height: 30
        text: 'btn3'

    Widget:
inclement
  • 29,124
  • 4
  • 48
  • 60
7

It is possible to use a StackLayout for this, to make sure the buttons go from top to bottom.

You could also try using padding with the BoxLayout

From the kivy BoxLayout docs:

Padding between layout box and children: [padding_left, padding_top, padding_right, padding_bottom].

padding also accepts a two argument form [padding_horizontal, padding_vertical] and a one argument form [padding].

Changed in version 1.7.0: Replaced NumericProperty with VariableListProperty.

padding is a VariableListProperty and defaults to [0, 0, 0, 0].

For instance, yours might look like:

BoxLayout:
    orientation: "vertical"
    width: 200
    size_hint_x: None
    padding: 0, 0, 0, bottom_padding_here

What you set it to, so that it always puts the buttons in the right place, no matter the screen size, is another matter. But totally doable I believe.

If you were to add or remove buttons at some later point, you would adjust the padding etc.

Totem
  • 7,189
  • 5
  • 39
  • 66
0

Use GridLayout with cols: 1 instead of BoxLayout

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Alexey
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 19 '22 at 10:46