Straightfw
Straightfw

Reputation: 2211

Setting grid column width in Kivy's GridLayout?

I'm currently creating a very simple GUI app which needs a few button on the left (in a narrow pane) and a matplotlib chart on the right (the wider one). I'm not experienced with Python GUIs but I decided to go with Kivy and tried something like this:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

class MyApp(App):

    def build(self):
        layout = GridLayout(cols=2)
        inLayout = GridLayout(rows=3)

        inLayout.add_widget(Button(text='Function 1', width = 50))
        inLayout.add_widget(Button(text='Function 2', width = 50))
        inLayout.add_widget(Button(text='Function 3', width = 50))

        layout.add_widget(inLayout)
        layout.add_widget(Button(text='Chart pane'))

        return layout


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

Since the buttons are the only things needed to be located in the left pane, I thought that maybe resizing all of them will narrow the whole pane but it doesn't. Is there any way to do this or do the cells in a GridLayout always have to be the same size, no matter what? If so, is there a similar layout in Kivy that would allow such alteration?

Upvotes: 2

Views: 8231

Answers (1)

Nykakin
Nykakin

Reputation: 8747

You should set a size_hint property of GridLayout children. In this case you need to alter inLayout object, a first child of layout instance. To have button always resize to 0.2 width of available space, set it to (0.2, 1):

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

class MyApp(App):

    def build(self):
        layout = GridLayout(cols=2)
        inLayout = GridLayout(rows=3, size_hint=(0.2, 1))

        inLayout.add_widget(Button(text='Function 1'))
        inLayout.add_widget(Button(text='Function 2'))
        inLayout.add_widget(Button(text='Function 3'))

        layout.add_widget(inLayout)
        layout.add_widget(Button(text='Chart pane'))

        return layout


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

To have a fixed size width set size_hint to (None, 1) and set `width' property:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

class MyApp(App):

    def build(self):
        layout = GridLayout(cols=2)
        inLayout = GridLayout(rows=3, width=100, size_hint=(None, 1))

        inLayout.add_widget(Button(text='Function 1'))
        inLayout.add_widget(Button(text='Function 2'))
        inLayout.add_widget(Button(text='Function 3'))

        layout.add_widget(inLayout)
        layout.add_widget(Button(text='Chart pane'))

        return layout


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

Upvotes: 2

Related Questions