GridLayouts in Kivy | Python (original) (raw)

Last Updated : 20 Jan, 2026

GridLayout in Kivy places widgets into a table-like structure made of rows and columns. Each widget is automatically placed into the next available cell based on the order in which it is added.

**Example: In this example, a TextInput is placed inside a GridLayout to show how a single widget fits inside a grid cell.

Python `

from kivy.app import App from kivy.uix.gridlayout import GridLayout from kivy.uix.textinput import TextInput

class Main(App): def build(self): g = GridLayout(cols=1) t = TextInput(text="Enter name") g.add_widget(t) return g

Main().run()

`

**Output

GridLayout

A single text input box appears in the window.

**Explanation:

Syntax

GridLayout(rows=None, cols=None)

**Parameters:

Examples

**Example 1: In this example, four buttons are arranged in a 2×2 grid with equal size.

Python `

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

class Main(App): def build(self): g = GridLayout(cols=2) g.add_widget(Button(text="A")) g.add_widget(Button(text="B")) g.add_widget(Button(text="C")) g.add_widget(Button(text="D")) return g

Main().run()

`

**Output

GridLayout

Four buttons appear in two rows and two columns.

**Explanation:

**Example 2: In this program, all rows are given a fixed height.

Python `

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

class Main(App): def build(self): g = GridLayout(cols=2, row_force_default=True, row_default_height=40) g.add_widget(Button(text="A")) g.add_widget(Button(text="B")) g.add_widget(Button(text="C")) g.add_widget(Button(text="D")) return g

Main().run()

`

**Output

Output1

All rows appear with the same fixed height.

**Explanation: