Table of contents
1.
Introduction
2.
Columns and Rows
3.
Example
4.
Frequently asked Questions
4.1.
What is Kivy?
4.2.
What is a canvas in KIVY?
4.3.
What is a widget in KIVY?
4.4.
What is the relative layout in KIVY?
4.5.
Which class is used for defining layout in KIVY?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Grid Layout

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Kivy is a Python platform-independent GUI tool. Because it can run on Android, iOS, Linux, and Windows, among other platforms, it is mainly used to construct Android applications, but it may also be used to develop desktop programs.

GridLayout in Kivy is a function used to display widgets in matrix format. To use Grid Layout, we first need to install the Kivy library. Type the following command to install Kivy.

pip install kivy

Columns and Rows

The grid layout uses the available space to divide into rows and columns. The location of each child is automatically determined by layout configuration. A grid layout must have at least one input constraint, i.e., cols and rows. If we do not specify the cols or rows, the layout gives you an exception. 

  • Columns represent the width, and the rows represent the height, just like the matrix. 
  • Initial, the size is given 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 cause the layout to ignore children's width and size_hint properties and use the default size.
  • To customize the size of a single column or row, use cols_minimum or rows_minimum.
  • It is not necessary to give both rows and columns. It depends on the requirement. We can provide either both or anyone accordingly. The grid layout uses the available space to divide into rows and columns. The location of each child is automatically determined by layout configuration. A grid layout must have at least one input constraint, i.e., cols and rows. If we do not specify the cols or rows, the layout gives you an exception. 
  • Rows mean the height, and Columns indicate the width, the same as the matrix. 
  • Initially, the size is given by the row_default_height and col_default_width properties. We can make the default size by setting the col_force_default or row_force_default property. We force the layout to ignore children's size_hint properties and width and use the default size.
  • We customize the size of a single row or column by using rows_minimum or cols_minimum.
  • It is not mandatory to provide both columns and rows. We can give either both or anyone accordingly on the requirement basis.

Example

In the following example, all the widgets will be the same or equal size. The size is (1, 1) by default so that the child will take the total length of the parent. We have to make the main.py python file for python code and run it using the command "python main.py" in the command prompt, and after that, we will run another file, my.kv file. 

We will write the following code in main.py

# Importing  the kivy module
import kivy

# Base Class of our App must inherit from the App class.
from kivy.app import App

#importing GridLayout
from kivy.uix.gridlayout import GridLayout
# Given class stores the info of .kv file when it is called goes to my.kv file
class Main(GridLayout):
	pass
	
# Definies the Base Class of our Kivy App
class Myapp(App):
	def build(self):
		# return a Main() as a root widget
		return Main()
		
if __name__ == '__main__':
	# Here the class MyApp is initialized
	# and its run() method called.
	MyApp().run()
You can also try this code with Online Python Compiler
Run Code

 

my.kv

# my.kv file code here
<Main>:
    cols: 2 
    rows: 2
    Button:       
         text: 'Box1'
    Button:
        text: 'Box2'
    Button:
        text: 'Box3'
    Button:       
        text: 'Box4'
You can also try this code with Online Python Compiler
Run Code

 

Output

Now let’s fix the buttons size to 100px instead of the default size_hint_x = 1.

# Just do change in the above my.kv
<MainWidget>:

cols: 2
rows: 2

Button:
text: 'Box1'
size_hint_x: None
width: 100
Button:
text: 'Box2'
Button:
text: 'Box3'
size_hint_x: None
width: 100
Button:
text: 'Box4'
You can also try this code with Online Python Compiler
Run Code

 

Output

Frequently asked Questions

What is Kivy?

Kivy is an open-source, cross-platform Python framework for developing applications that use innovative, multi-touch user interfaces. 

What is a canvas in KIVY?

The Canvas in Kivy is the root object used for drawing by a widget. It is not the place where we paint. Every Widget in kivy already has a canvas when we create a widget and make all the instructions needed for drawing.

What is a widget in KIVY?

A Widget in Kivy is the base building block of GUI interfaces in Kivy. It gives a canvas that can be used to draw on the screen. It receives events and reacts to them.

What is the relative layout in KIVY?

This relative layout allows us to set relative coordinates for the children. If we want absolute positioning, we should use the FloatLayout. The RelativeLayout class works like the regular FloatLayout, except its child widgets are positioned relative to the layout.

Which class is used for defining layout in KIVY?

PageLayout is used to define and create simple multi-page layouts that allow easy flipping from one page to another using a border.

Conclusion

In this article, we have extensively discussed the Grid Layout in Kivy. This article outlines how to place widgets inside a grid layout. And at the end of the blog, we have seen some examples to understand Grid Layout better. 

After reading about the Grid Layout in Kivy, are you not feeling excited to read/explore more articles on the topics of kivy? Don’t worry, Coding ninjas has you covered. To learn more on such topics, you can visit StackLayout in Kivy using .kv fileFloatLayout in Kivy using .kv fileBoxLayout Widget in KivyThe drop-down List In Kivy, and Background template in Kivy!

Refer to our guided path on Coding Ninjas Studio to upskill yourself in Data structure and algorithmsCompetitive Programming, JavascriptSystem Design, and many more if you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass