Need help understanding size_hint in kivy
Asked Answered
P

1

8

I was was wondering if I get get an explanation on how size_hint works in Kivy. From my understanding, it is the relative scale from the widget, to its parent layout. I tried the following code:

    class TestFrame(GridLayout):
        def __init__(self, **kwargs):
            GridLayout.__init__(self, **kwargs)
            self.rows = 1
            self.add_widget(Label(text='test_num', size=(100, 25), size_hint=(.10, None)))
            self.add_widget(Label(text='test_txt', size=(100, 25), size_hint=(.75, None)))
            self.add_widget(Button(text='test_btn', size=(100, 25), size_hint=(.15, None)))

This is what I expect:

    |----------------------------------------------------------------------|
    |Test_num :                       Test_txt                :  Test_btn  |

Instead this is the result:

    |----------------------------------------------------------------------|
    |                                    Test_num :  Test_txt   : Test_btn |

I've played with different combinations of size_hint and size and end up with very similar results. What am I missing or not understanding?

Patricepatrich answered 20/10, 2015 at 0:10 Comment(0)
G
9

As far as I am aware, use of size_hint AND size are incompatible, when you set them for the same attribute, as in setting x or y for both. Use one or the other. If you think about it, it makes sense. With size, you are explicitly setting a size for the widget, and with size_hint, you are also explicitly setting a size for the widget, just in a different way, that being relative to the widgets parent. So if you use both for the same attribute, x or y, they are bound to conflict.

Exceptions here are probably when you have for instance, size_hint_y set to None, like you do above, and then specify a size for y. Or if you set size_hint_x to None, and only specified a size for x.

However, above you have set the size of x, and the size_hint of x in all instances.

So: These should be ok

size_hint=(None, .5), width=100
size_hint=(.5, None), height=100

These would conflict

size_hint=(.6, .5), size=(34, 66)
size_hint=(None, 55), height=80
Gratian answered 20/10, 2015 at 11:52 Comment(2)
I just tried that again and it did not make a difference.Indices
Ugh I feel like an idiot, I had another widget that at the top of the class that I overlooked and it did not include size_hint. Because I didn't use size_hint on all of them it wasn't working correctly.Indices

© 2022 - 2024 — McMap. All rights reserved.