Python | Grid layout in kiws without .kv file



Kivy is platform independent as it can run on Android, IOS, Linux, Windows, etc. Kivy provides you with the functionality to write code once and run it on different platforms. It is mainly used for developing Android application, but that does not mean that it cannot be used in desktop applications.

Grid layout:

  • The widget must be placed in a specific column / row. Each child is automatically assigned a position based on the layout configuration and the child`s index in the child list.
  • The grid layout must always contain one of the following input constraints:
    GridLayout.cols or GridLayout.rows. If you don`t specify columns or rows, the layout will throw an exception.
  • The GridLayout places children in a matrix. It takes up the available space and divides it into columns and rows, then adds widgets to the resulting “cells”.
  • The row and columns are the same as we see in the matrix, here we can adjust the size of each grid.
  • The initial size is set by the col_default_width and row_default_height properties. We can force the default size by setting the col_force_default or row_force_default property. This will force the layout to ignore the width and size_hint properties of the children and use the default size.

The first thing we need to do to use the GridLayout is import it.

 from kivy.uix.gridlayout import GridLayout 

A basic approach to creating a GridLayout:

 1) import kivy 2) import kivyApp 3) import button 4) import Gridlayout 5) Set minimum version (optional) 6) create App class: - define build function: add widget (Buttons) 7) return Layout / widget / Class (according to requirement) 8) Run an instance of the class 

Implementing the —

Code # 1:
In the example below, all widgets will be the same size. By default, size_hint is (1, 1), so the widget will have the full size of the parent:

# Sample Python application demonstrating
# How to create a Qiwi GridLayout

 
# import kivy module

import kivy 

 
# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy.app import App 

 
# cos gives a button in kiw
# if not imported shows an error

from kivy.uix.button import Button

 
# GridLayout arranges children in a matrix.
# It takes up available space and
# divides it into columns and rows,
# then adds widgets to the resulting "Cells".

from kivy.uix.gridlayout import GridLayout

  
# create application class

class Grid_LayoutApp (App):

  

# we need to create an application

# return the widget to the build () function.

  def build ( self ):

 

# add GridLayouts to the application

# Identify the number columns

# You can use a string as well as needed

layout = GridLayout (cols = 2 )

  

# 1st row

layout.add_widget (Button (text = `Hello 1` ))

  layout.add_widget (Button (text = `World 1` ) )

 

# 2nd row

  layout.add_widget (Button (text = `Hello 2` ))

layout.add_widget (Button (text = ` World 2` ))

 

# 3rd row

layout.add_widget (Button (text = `Hello 3` ))

layout.add_widget (Button (text = `World 3` ))

  

# 4th row

layout.add_widget (Button (text = `Hello 4` ))  

layout.add_widget (Button (text = `World 4` ))

  

# return layout

return layout

  
# create an App class

root = Grid_LayoutApp ()

# launch the application
root.run ()

Output:

Now just change the class code in the above code with code # 2 and code # 3, except they will all be the same as code # 1 and run the code after the changes, you will get the following results.

Code # 2:
Now let`s set the Hello buttons to 100px instead of using size_hint_x = 1:

Output:

Code No. 3:
Now let`s set the line-height to a specific size:

# create application class

class Grid_LayoutApp (App):

 

  # to create the application we need

# return the widget to the build () function.

  def build ( self ):

 

# add GridLayouts to the application

# Column number definition

# You can use a string, but also as required

  layout = GridLayout (cols = 2 )

 

# 1st row

layout.add_widget (Button (text = ` Hello 1 ` , size_hint_x = None , width = 100 ))

layout.add_widget (Button (text = ` World 1` ))

 

# 2nd row

layout.add_widget (Button (text = `Hello 2` , size_hint_x = None , width = 100 ))

layout.add_widget (Button (text  = `World 2` ))

 

# 3rd row

layout.add_widget (Button (text = `Hello 3` , size_hint_x = None , width = 100 ))

layout.add_widget (Button ( text = `World 3` ))

 

# 4th row

  layout.add_widget (Button (text = ` Hello 4 ` , size_hint_x = None , width = 100 ))

layout.add_widget (Button (text = `World 4` ))

  

# return layout

return layout

# create application class

class Grid_LayoutApp (App):

 

# we need to create an application

# return the widget to the build () function.

  def build ( self ):

 

# adding GridLayouts to your application

# Determination of the number of columns and the size of the buttons, ie. heights

layout = GridLayout (cols = 2 , row_force_default = True ,

row_default_height = 30 )

 

# 1st row

  layout.add_widget (Button (text = ` Hello 1` , size_hint_x = None , width = 100 ))

  layout.add_widget (Button (text = `World 1` ))

 

# 2nd row

  layout.add_widget (Button (text = ` Hello 2` , size_hint_x = None , width = 100 ))

layout.add_widget (Button (text = ` World 2`  ))

 

# 3rd row

layout.add_widget (Button (text = `Hello 3` , size_hint_x = None , width = 100 ))

layout.add_widget (Button (text = ` World 3` ))

 

# 4th row

layout.add_widget (Button (text `Hello 4` , size_hint_x = None , width = 100 ))

layout.add_widget (Button (text = `World 4` ))

 

# return layout

return layout

Output:

Link: https://kivy.org/doc/st able / api-kivy.uix.gridlayout.html