Search code examples
pythonuser-interfacelayoutkivy

How to design an interface with an irregular grid layout


I'm trying to design an interface in Python with Kivy. I need to add widgets to my App in a precise scheme, let's say a grid with two rows and three columns. I would not add widgets in all six positions. I am not sure that the GridLayout is the most suitable, so I started modifying a more complex layout.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string("""
<Boxes>:
   AnchorLayout:
       anchor_x: 'center'
       anchor_y: 'top'
       BoxLayout: 
           orientation: 'vertical'
           padding: 20
           BoxLayout:
               orientation: 'horizontal'
               Button:
                   text: "1"
               Button:
                   text: "2"
               Button:
                   text: "3"
           BoxLayout:
               orientation: 'horizontal'
               Button:
                   text: "4"
               Button:
                   text: "6" """)


class Boxes(FloatLayout):
   pass

class TestApp(App):
   def build(self):
       return Boxes()

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

This code generate this layout:

enter image description here

I would like to have the button "4" in the first column of the second row, and the button "6" in the third column of the second row, thus giving space to another button not currently added. The button "4" and "6" should be aligned with buttons "1" and "3" respectively. Any suggestion? Which is the most suitable layout for an irregular grid scheme? Is there a way to add widgets in a Kivy grid layout specifying their position in terms of row and column?


Solution

  • You can just add a Widget to take up the space that you want blank:

            BoxLayout:
                orientation: 'horizontal'
                Button:
                    text: "4"
                Widget:
                Button:
                    text: "6" """)