简体   繁体   English

需要帮助理解kivy中的size_hint

[英]Need help understanding size_hint in kivy

I was was wondering if I get get an explanation on how size_hint works in Kivy. 我当时想知道我是否得到了关于size_hint如何在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. 我玩过size_hint和size的不同组合,结果非常相似。 What am I missing or not understanding? 我错过了什么或不理解?

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. 据我所知,当你为同一属性设置size_hintsize时,使用size_hintsize是不兼容的,就像设置两者的x或y一样。 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. 使用size ,您可以显式设置窗口小部件的大小,并且使用size_hint ,您还可以以不同的方式显式设置窗口小部件的大小,即相对于窗口小部件父级。 So if you use both for the same attribute, x or y, they are bound to conflict. 因此,如果将两者用于相同的属性x或y,则它们必然会发生冲突。

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. 例如,当您将size_hint_y设置为None时,可能就像上面那样,然后指定y的size Or if you set size_hint_x to None, and only specified a size for x. 或者,如果将size_hint_x设置为None,并且仅指定x的size

However, above you have set the size of x, and the size_hint of x in all instances. 但是,在上面你已经设置了x的size ,并且在所有实例中都设置了x的size_hint

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM