【Kivy】三、布局学习——1、BoxLayout布局的学习

一、说明

Box.Layout 称为盒子布局,是一种可以将子部件水平或垂直排列的布局。在Kivy 中,可以使用
BoxLayout 进行水平或垂直的排列子部件,它类似千Android 中的线性布局。如果不设置任何大小,子
部件将会以10 像素的间距平分父窗口的大小。

二、代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###---------------------1、文件说明---------------------###
'''
* 说明:BoxLayout学习demo
* 时间:2021-4-17
* 作者:宏沉一笑
* 备注:
'''
###----------------------2、库导入----------------------###
# 导入Kivy 的App类,它是所有Kivy应用的基类
from kivy.app import App
# 引入BoxLayout布局
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string('''
<MyLayout>:
    orientation: "horizontal" # 横向的排列
    BoxLayout:
        orientation: "vertical" # 纵向的排列
        canvas:
            Color:
                rgba: [1, 0, 0, 1] # 红色
            Rectangle:
                size: self.size
                pos: self.pos  
        Button:
            text: "butt0"
        Button:
            text: "butt0"
        Button:
            text: "butt0"
    BoxLayout:
        canvas:
            Color:
                rgba: [0, 1, 0, 1] # 绿色
            Rectangle:
                size: self.size
                pos: self.pos
        spacing: 10 # 按钮之间
        Button:
            text: "butt0"
        Button:
            text: "butt0"
        Button:
            text: "butt0"
    BoxLayout:
        canvas:
            Color:
                rgba: [0, .4, 0, 1] # 橙色
            Rectangle:
                size: self.size
                pos: self.pos  
        padding: [10, 20] # 横间距 纵间距
        Button:
            text: "butt0"
        Button:
            text: "butt0"
        Button:
            text: "butt0"
    BoxLayout:
        canvas:
            Color:
                rgba: [1, .6, 0, 1] # 橙黄
            Rectangle:
                size: self.size
                pos: self.pos  
        padding: [0, 10, 20, 30] # 左 上 右 下
        spacing: 10
        Button:
            text: "butt0"
        Button:
            text: "butt0"
        Button:
            text: "butt0"
''')
class MyLayout(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

class MyApp(App):
    def build(self):
        return MyLayout()   # Kivy将自动缩放根控件,

if __name__ == "__main__":
    MyApp().run()
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值