0

我是 Kivy 的新手,我已经尝试了几天来寻找合适的布局,但我似乎没有得到结果。我希望图片中的按钮“2”和“3”保持在按钮“1”和“4”等角落。我应该怎么办?

http://i.stack.imgur.com/Y6Rjo.png

这是我的代码,但它不能按需要工作:

# Main
BoxLayout:
    size_hint: 1, .85
    # Outer
    canvas:
        Color:
            rgba: 1, 1, 1, .3
        Rectangle:
            pos: self.pos
            size: self.size
    # Inner
    BoxLayout:
        AnchorLayout:
            canvas:
                Color:
                    rgba: 1, 1, 1, .6
                Rectangle:
                    pos: self.center_x / 2, self.center_y / 2
                    size: self.width / 2, self.height / 2
            BoxLayout:
                size_hint: .5, .5
                AnchorLayout:
                    anchor_x: 'left'
                    anchor_y: 'top'
                    Button:
                        size_hint: None, None
                        text: '1'

                AnchorLayout:
                    anchor_x: 'right'
                    anchor_y: 'top'
                    Button:
                        size_hint: None, None
                        text: '2'

                AnchorLayout:
                    anchor_x: 'left'
                    anchor_y: 'bottom'
                    Button:
                        size_hint: None, None
                        text: '3'

                AnchorLayout:
                    anchor_x: 'right'
                    anchor_y: 'bottom'
                    Button:
                        size_hint: None, None
                        text: '4'
4

1 回答 1

1

我建议将这些按钮放在相对布局上,然后操纵它们的pos_hint属性进行定位。截屏:在此处输入图像描述

代码:

#!/usr/bin/env python3.5
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.lang import Builder

gui = '''
Screen

    RelativeLayout
        size_hint: None, None
        size: 500, 500
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}

        canvas:
            Color:
                rgba: 1, 1, 1, 0.3
            Rectangle:
                pos: 0, 0
                size: self.size

        MyButton
            pos_hint: {'left': 1, 'top': 1}
            text: 'top left'

        MyButton
            pos_hint: {'right': 1, 'top': 1}
            text: 'top right'

        MyButton
            pos_hint: {'left': 1, 'bottom': 1}
            text: 'bottom left'

        MyButton
            pos_hint: {'right': 1, 'bottom': 1}
            text: 'bottom right'

<MyButton@Button>
    size_hint: None, None
    size: 100, 100
'''


class Test(App):

    def build(self):
        return Builder.load_string(gui)


Test().run()
于 2016-08-30T11:37:52.053 回答