一、说明
Anchor Layout 为错点布局。在使用AnchorLayout 布局时,可以将子部件设置在左上、中上、右
上、左中、正中、右中、左下、中下、右下共9 个位置处。AnchorLayout 布局的使用方法也很简单,
只需要指定anchor_x 和anchor_y 两个属性即可。
二、代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###---------------------1、文件说明---------------------###
'''
* 说明:AnchorLayout布局学习demo
* 时间:2021-4-17
* 作者:宏沉一笑
* 备注:
'''
###----------------------2、库导入----------------------###
# 导入Kivy 的App类,它是所有Kivy应用的基类
from kivy.app import App
# 引入布局
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang import Builder
Builder.load_string('''
<MyLayout>:
padding: 5
AnchorLayout:
anchor_x: 'left'
anchor_y: 'top'
Button:
text: "button"
size_hint: .3, .2
AnchorLayout:
anchor_x: 'right'
anchor_y: 'bottom'
Button:
text: "button2"
size_hint: .1, .2
''')
class MyLayout(AnchorLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class MyApp(App):
def build(self):
return MyLayout() # Kivy将自动缩放根控件,
if __name__ == "__main__":
MyApp().run()