user3024655
user3024655

Reputation:

GUI : How to use GridLayout using kivy in python?

I am trying to make a simple GUI in python using kivy and some widgets. However I encountered some problem for putting on exact location of these widgets (I unsuccessfully used BoxLayout). The design of the GUI is given in the figure joined with this comment. Does somebody can help me to draw this with kivy ? Thanks in advance.

enter image description here

Upvotes: 0

Views: 342

Answers (1)

Nykakin
Nykakin

Reputation: 8747

You need to nest some layouts. Some will be BoxLayouts with orientation vertical, some with orientation horizontal. Sometimes you will use GridLayout when you want to have a regular grid. You can use Widget for an empty space. Everywhere you set size_hint_x and size_hint_y (or both at once with size_hint) with proportions you want to keep. If you want to have fixed size, set these to None and set width/height accordingly. Here's some example for you to improve (used buttons as placeholders):

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string("""
<MyWidget>:
    orientation: 'vertical'
    BoxLayout:
        size_hint_y: 0.1
        TextInput:
            size_hint_x: 0.25
        Button:
            size_hint_x: 0.15
        Button:
            size_hint_x: 0.15
        Button:
            size_hint_x: 0.15
        Button:
            size_hint_x: 0.1
        Button:
            size_hint_x: 0.1
        Button:
            size_hint_x: 0.1
    Widget:
        size_hint_y: None
        height: 10
    BoxLayout:
        size_hint_y: 0.4
        Widget:
            size_hint_x: None
            width: 10
        GridLayout:
            cols: 10
            size_hint_x: 0.8
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
            Button
        Widget:
            size_hint_x: None
            width: 10
    Widget:
        size_hint_y: 0.4
""")


class MyWidget(BoxLayout):
    pass


class MyApp(App):
    def build(self):
        return MyWidget()

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

Upvotes: 1

Related Questions