Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GridLayout wrong positioning

I have my simple python code:

import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout


Builder.load_file('Matrixkivy.kv') 
 
class MyLayout(Widget):
    pass

class MatrixCalc(App):
    def build(self):
        return MyLayout()

if __name__ == '__main__':
    MatrixCalc().run()

And simple .kv code:

<MyLayout>
    GridLayout:
        size_hint: root.width, root.height
        Button:
            text: "Button1"
        Button:
            text: "Button2"
            
        

I want to get a GridLayout which is of size of the entire app and two buttons, which both take half of it. However, app displays all things that the app contain in the bottom left corner. How to fix that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The main problem is that you are using the base Widget class as a container. You can do that, but the Layout classes already have the code to handle things like size_hint and pos_hint. One fix for your code is to replace:

class MyLayout(Widget):
    pass

with:

class MyLayout(FloatLayout):
    pass

You should also remove the line:

size_hint: root.width, root.height

Size_hint values should be numbers typically in the range 0-1. And add a line that defines either rows or cols for the GridLayout, so your kv could look like:

<MyLayout>
    GridLayout:
        cols: 1
        Button:
            text: "Button1"
        Button:
            text: "Button2"

Since you are using GridLayout as a child of MyLayout, you can just make MyLayout extend GridLayout, and then you can eliminate the GridLayout line from your kv. With this approach, your MyLayout class would look like:

class MyLayout(GridLayout):
    pass

and the corresponding kv:

<MyLayout>
    cols: 1
    Button:
        text: "Button1"
    Button:
        text: "Button2"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading