Kivy AnchorLayout .Kv文件

时间:2018-10-13 07:14:13

标签: python kivy anchor layout-anchor

我是Kivy的新手。我希望我的标签在左侧,但是它始终位于中间,这是我做错了吗?

.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()

这是.kv文件的小片段。

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

感谢您提供任何有用的答案。

1 个答案:

答案 0 :(得分:0)

问题在于,子窗口小部件的默认大小是填充其父窗口小部件,因此您的anchor_xanchor_y无效。要解决此问题,只需将Label的大小设置为合理的值即可:

<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