Mahdi
Mahdi

Reputation: 1035

How can I use GridLayout in 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

Upvotes: 4

Views: 13648

Answers (2)

omar triki
omar triki

Reputation: 1

if you will use python3 raw_input function will not works

Upvotes: -1

tito
tito

Reputation: 13251

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.

Upvotes: 13

Related Questions