简体   繁体   中英

Python Kivy - pos_hint not updating in FloatLayout using code

I've been trying to adjust the position of this Label, but everytime I've adjusted the pos_hint it keeps the label in the middle of the window. I've searched a lot, but all of the results use the .kv file which I'm trying to avoid. I looked at the documentation on https://kivy.org/doc/stable/api-kivy.uix.floatlayout.html and it appears that I'm using it correctly, but can't figure this out to save my life. After printing out the pos_hint it appears that the variable is adjusting, just not adjusting on the screen.

import kivy
kivy.require('1.0.7')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout

class TestApp(App):
    def build(self):    
        f = FloatLayout()
        lab = Label(text="text", pos_hint={"x": 0, 'top':1})
        f.add_widget(lab)
        return f

if __name__ == '__main__':
    test = TestApp();
    test.run()

If you don't give constraints to your Label widget's size (height & width), it'll will try to become as big as its parent widget (in the case of using FloatLayout widget). You can try to set size_hint property of your Label widget and/or set the height and width property of your Label widget.

...
lab = Label(text="text", pos_hint={"x": 0, 'top':1})
lab.size_hint= (None, None)
lab.width= 100 # Label widget's width
lab.height = 56 # Label widget's height
f.add_widget(lab)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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