kivymd buttons remain on the left of the screen

40 Views Asked by At

I can't position my buttons in the center on kivymd, I tried first with a normal boxlayout and then the two buttons but they remain attached to each other, (I tried inserting two labels and they are positioned in the center without problems but with the buttons it's not like that) then I tried to make two layout boxes inside a layout box but one button was placed on the left (to the left of the first layout box which divides the page in half) and the other in the center (to the left of the second layout box ) how can I position them in the center?

kivy code:

        BoxLayout:
            orientation: "vertical" 
            BoxLayout: 
                orientation: "horizontal"
                BoxLayout:
                    
                    MDRectangleFlatButton:
                        halign: 'center'
                        pos_hint: {"center_x": .5, "center_y": .5}
                        text: "hi"
                       
                BoxLayout:
                    MDRectangleFlatButton:
                        text: "hi"
                        halign: 'center'
                        pos_hint: {"center_x": .5, "center_y": .5}
1

There are 1 best solutions below

2
John Anderson On

Try this:

BoxLayout:
    orientation: "horizontal" 
            
    AnchorLayout:
        MDRectangleFlatButton:
            halign: 'center'
            pos_hint: {"center_x": .5, "center_y": .5}
            text: "hi"
    AnchorLayout:
        MDRectangleFlatButton:
            text: "hi"
            halign: 'center'
            pos_hint: {"center_x": .5, "center_y": .5}

The MDRectangleFlatButton sets its size_hint to [None, None], so it does not fill the available space. The AnchorLayout leaves the default size_hint of [1, 1], so the two of them evenly share all the available space.