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?
Understanding the size_hint Property in Widget ¶ The size_hint is a tuple of values used by layouts to manage the sizes of their children. It indicates the size relative to the layout's size instead of an absolute size (in pixels/points/cm/etc).
pos_hint will make the values relative to the size/position of the parent. So here, for b1 x will be the x of the parent, and center_y will be at the middle between y and top . Now, if you run this, you may get a surprise, because FloatLayout also made b1 and b2 sizes relative to root.
Widgets in Kivy are organized in trees. Your application has a root widget , which usually has children that can have children of their own. Children of a widget are represented as the children attribute, a Kivy ListProperty .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With