Book Image

Kivy Cookbook

By : Hugo Solis
Book Image

Kivy Cookbook

By: Hugo Solis

Overview of this book

Table of Contents (16 chapters)
Kivy Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using GridLayout


GridLayout is the way to organize the child widgets in a table. In this recipe, we will organize four buttons in a 2x2 grid.

How to do it…

Again we will use the KV file to set the widgets and the Python file to set the layout. To complete the recipe:

  1. In the KV file, provide the number of columns and rows with column and row properties.

  2. Also, define four buttons using the following code:

    <MyW>:
        cols: 2
        rows: 2
        Button:
            id: label1
            text: 'B1'
        Button:
            id: label2
            text: 'B2'
        Button:
            id: label3
            text: 'B3'
        Button:
            id: label4
            text: 'B4'
  3. In the Python file, import GridLayout.

  4. Define the root class as GridLayout. Use the following code to do that:

    import kivy
    
    from kivy.app import App
    from kivy.uix.gridlayout import GridLayout
    
    class MyW(GridLayout):
        pass
    
    class e8App(App):
        def build(self):
            return MyW()
    
    if __name__ == '__main__':
        e8App().run()

How it works…

In the KV file, we are...