Can't understand size_hint. Why this .kv simply code doesn't work as i expected?

2.2k Views Asked by At

I'm studying Kivy library but i've some problems. I'm trying to do a simple screen-changing app in order to do some exercise but I'm doing some errors here I think :

                    BoxLayout:
                        orientation:"horizontal"
                        size_hint:1,0.5
                        Label :
                            text : "Nome :"
                        TextInput :
                            id : _name
                            multiline: False
                        Label :
                            text : "Cognome :"
                        TextInput :
                            id : _surname
                            multiline: False

The code is :

<ROT>:
    FloatLayout:
        AnchorLayout:
            anchor_y : "top"
            anchor_x: "right"
            BoxLayout:
                orientation : "horizontal"
                spacing: 10
                size_hint: 0.4,0.1
                Button:
                    id : "_bfr"
                    text : "<-----"

                Button:

                    id : "_aft"
                    text : "----->"
        AnchorLayout :
            anchor_x : "center"
            anchor_y : "bottom"
            ScreenManager:
                size_hint_y : 0.9
                id : _manager
                Screen :
                    name : "uno"
                    BoxLayout :
                        padding :50
                        spacing :10
                        orientation : "vertical"
                        BoxLayout:
                            orientation:"horizontal"
                            size_hint:1,0.5
                            Label :
                                text : "Nome :"
                            TextInput :
                                id : _name
                                multiline: False
                            Label :
                                text : "Cognome :"
                            TextInput :
                                id : _surname
                                multiline: False

I want the last written boxlayout to be 1/2 of the Screen that includes it. I can't understand if the

size_hint

is linked with the dimension of the window or with the dimension of the "parent" that includes that widget

1

There are 1 best solutions below

1
On

Size_hints reference your parent widget usually. In this case, the BoxLayout is 0.5 of your ScreenManager which is 0.9 of your window. In this case the BoxLayout is 45% of your window vertically.

If you want your BoxLayout to take exactly 50% of your screen, you can reference your root widget size (the very top parent wrapped in the arrows)


size_hints typically reference the immediate parent widget. However you can change this by referencing your root widget directly (the top widget wrapped in arrows) by writing something like this:

<Root>:
    WidgetLayout1: 
        size_hint: .2, .2
        BoxLayout:
            size_hint: root.width, root.height / 2

In this case the BoxLayout is bigger than its parent because its size is equal to the whole width of the root widget, and half its height.

However in your example, your second box is inside another BoxLayout that has padding and spacing, which has its own rules about spacing and sizing that will probably overrule your own defined size hints. Not to mention the other rules of AnchorLayout which will restrict you even further to certain areas of the screen.

You can read more about BoxLayout here.