pythonkivyanchorlayout-anchor

Kivy AnchorLayout .Kv file


I am new to Kivy. I want my Label to be on the left side, however it stays always in the centre, what I'm doing wrong?

.py `

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.label import Label


class Display(BoxLayout):
    pass

class Screen_One(Screen):
    pass

class Screen_Two(Screen):
    pass

class Screen_Three(Screen):
    pass

class DemoApp(App):
    icon = 'icon.png'
    title = 'control panel'
    def build(self):
        return Display()

if __name__ == '__main__':
    DemoApp().run()

Here is a litte snippet of the .kv file.

<Screen_Two>:
    name: 'screen_two'
#    BoxLayout:
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'top'
        Label:
            text: 'test box position'

Thanks for any answer that is usefull.


Solution

  • The problem is that the default size for a child widget is to fill its parent, so your anchor_x and anchor_y have no effect. To fix it, just set the size of your Label to something reasonable:

    <Screen_Two>:
        name: 'screen_two'
        AnchorLayout:
            anchor_x: 'left'
            anchor_y: 'top'
            Label:
                text: 'test box position'
                size_hint: (None, None)
                size: self.texture_size