Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use GridLayout in Kivy?

Tags:

python

kivy

I want to use simple grid layout in my kivy program, but I don't appropriate example;

here is my code:

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

 layout = GridLayout(cols=2, row_force_default=True, row_default_height=40)
 layout.add_widget(Button(text='Hello 1', size_hint_x=None, width=100))
 layout.add_widget(Button(text='World 1'))
 layout.add_widget(Button(text='Hello 2', size_hint_x=None, width=100))
 layout.add_widget(Button(text='World 2'))

 raw_input()

At the end I cant see any result (Just single white window, without any grid)

Thanks for your attention

like image 833
Mahdi Avatar asked Dec 16 '22 22:12

Mahdi


1 Answers

You missed to create an initial App class, and run it:

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

class MyApp(App):
    def build(self):
        layout = GridLayout(cols=2, row_force_default=True, row_default_height=40)
        layout.add_widget(Button(text='Hello 1', size_hint_x=None, width=100))
        layout.add_widget(Button(text='World 1'))
        layout.add_widget(Button(text='Hello 2', size_hint_x=None, width=100))
        layout.add_widget(Button(text='World 2'))
        return layout

MyApp().run()

If you use raw_input(), you'll break the application loop. If you want to do interactive coding, look at kivy.interactive module.

like image 99
tito Avatar answered Dec 31 '22 21:12

tito