Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center buttons in Kivy?

Tags:

python

kivy

I am new to Python UI programming, and I am trying out Kivy. I want to center some buttons on my screen, but the buttons do not move from the bottom right of the window. I was wondering if anyone could point out what I am doing wrong or am missing?

vgx.kv:

#:kivy 1.9.1

<VgxMainScreen>:
    BoxLayout:
        size_hint: 0.2,0.2
        size: 200, 100
        orientation: 'vertical'

        Button:
            text: 'Game Select'
        Button:
            text: 'Options'
        Button:
            text: 'Controllers'

<VgxUI>:
    AnchorLayout:
        anchor_x: 'center'
        VgxMainScreen:
            size_hint: 0.2,0.2

<VgxApp>:
    FloatLayout:
        VgxUI:
            center: 0.5, 0.5

VgxApp.py:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
kivy.require('1.9.1')

class VgxMainScreen(Widget):
    pass

class VgxUI(Widget):
    pass

class VgxApp(App):
    def build(self):
        return VgxUI()

if __name__ == '__main__':
    VgxApp().run()
like image 309
Jose Avatar asked Feb 23 '16 08:02

Jose


People also ask

How do I center my widgets on KIVY?

You can use RelativeLayout and all widgets inside it will position relatively to this layout.

How do I resize a button on KIVY?

To change the size and position of a button in kivy, we use four properties: size, pos,size_hint, and post_hint. Out of these four properties, size and pos properties are used for static placement of widgets and size_hint and pos_hint properties for dynamic placement of the widgets.

How does the Pos_hint work in KIVY?

pos_hint will make the values relative to the size/position of the parent. So here, for b1 x will be the x of the parent, and center_y will be at the middle between y and top . Now, if you run this, you may get a surprise, because FloatLayout also made b1 and b2 sizes relative to root.

What is POS hint in KIVY?

pos : pos stands for position i.e it is used to position the widget. By default (0, 0), the bottom-left corner of the screen is the default position of the button in kivy python.


1 Answers

Edit: Best way to debug what's going on with you widgets is to change their background colors :).

canvas.before:
    Color:
        rgba: X, X, X, 1
    Rectangle:
        pos: self.pos
        size: self.size

The problem was that your BoxLayout wasn't positioned relatively to its parent even though it was inside it.

enter image description here

What I did here is that I positioned the BoxLayout to its parent's size and position using:

size: self.parent.size
pos: self.parent.pos

I also moved the size property from VgxMainScreen to VgxUI because I assume this is more common use-case (you can have multiple VgxMainScreens each with different size). Full code with background colors:

#:kivy 1.9.1

<VgxMainScreen>:
    size_hint: None, None
    canvas.before:
        Color:
            rgba: 1, 0, 0, 1  # red
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        canvas.before:
            Color:
                rgba: 0, 0, 1, 1  # blue
            Rectangle:
                pos: self.pos
                size: self.size

        size: self.parent.size  # important!
        pos: self.parent.pos  # important!
        orientation: 'vertical'

        Button:
            text: 'Game Select'
        Button:
            text: 'Options'
        Button:
            text: 'Controllers'

<VgxUI>:
    AnchorLayout:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1  # white
            Rectangle:
                pos: self.pos
                size: self.size
        size: self.parent.size
        anchor_x: 'center'
        anchor_y: 'center'
        VgxMainScreen:
            size: 200, 100

<VgxApp>:
    FloatLayout:
        VgxUI:
            center: 0.5, 0.5

enter image description here

There's yet another solution to this.

You can use RelativeLayout and all widgets inside it will position relatively to this layout.

<VgxMainScreen>:
    size_hint: None, None

    RelativeLayout:
        pos: self.parent.pos
        size: self.parent.size

        BoxLayout:
            orientation: 'vertical'
            Button:
                text: 'Game Select'
            Button:
                text: 'Options'
            Button:
                text: 'Controllers'

I've personally scratched my head many times when using Kivy and wondered why my widgets aren't positioned as I think they should :).

like image 153
martin Avatar answered Oct 05 '22 08:10

martin