Amin Etesamian
Amin Etesamian

Reputation: 3699

Kivy putting buttons on the corner of layout

I'm new to Kivy and I've been trying for a couple of days for a suitable layout but I dont seem to get a result. I want the buttons '2' and '3' in the picture to stay on corners like buttons '1' and '4'. what should I do?

https://i.sstatic.net/Y6Rjo.png

here is my code but it doesnt work as needed:

# 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'

Upvotes: 0

Views: 3246

Answers (1)

jligeza
jligeza

Reputation: 4693

I would advise putting these buttons on a relative layout, and then manipulating their pos_hint properties for positioning. Screenshot:enter image description here

Code:

#!/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()

Upvotes: 1

Related Questions