Tkinter Grid

“Creating and designing a GUI (Graphical User Interface) using a programming language has never been easier. However, the tkinter library offers some incredible and useful functions to design and create GUI applications quickly and easily. It offers several built-in functions that can automatically create widgets for a GUI application. In this guide, we will explore the tkinter grid() function provided by the tkinter library to design a GUI application. By the end of this tutorial, you will be able to use the grid geometry function in your python program.”

What is a Tkinter Grid?

In general, the grid is a framework of network lines that cross each other and are parallel to each other, making a series of rectangular or square windows. The grid geometry function provided by the tkinter library follows the same definition of the grid.

The grid geometry manager is one of the most flexible, simple, and easiest geometry managers distributed by the tkinter library. It allows you to put the widgets in a two-dimensional table. It arranges the rows and columns parallelly to make a widget of a square or rectangular shape. For example, if you have a grip of 2×2 size, it will look like this:


Each intersection of columns and rows is known as a “Cell,” which can hold a widget. An index number (row, column) represents each cell in the grid. So, the 2×2 size grid will be represented by (0, 0), (0, 1), (1, 0), and (1, 1). By default, the first row and first column have a zero index, the second row and second column have 1 index, and so on.

The indexes of the grid do not have to be in a sequence, and they may have gaps as well. For example, you want to add different widgets with the grid, so your grid may look like (1, 1), (2, 3), (5, 1), (7, 7), (9, 8), and (10, 10).

Now let us write some codes that will help us use the grid geometry function in our python program. Make sure to run these example programs for a better understanding.

Example 1

In this instance, we’ll show you a 2×2 grid. We will define the label for each cell as (0, 0), (0, 1), (1, 0), and (1, 1) to show you how the grid has placed them in order. First, we make a root module Tk() in order to get a display environment. After that, we will use the label() function in order to define the text for each cell in the grid. And then, we will use the grid() function to place each label() in the cell of a grid(). Look at the code provided below:

from tkinter import *
from tkinter.ttk import *
r = Tk()
l1 = Label(r, text ="(0,0)")
l2 = Label(r, text ="(0,1)")
l3 = Label(r, text ="(1,0)")
l4 = Label(r, text ="(1,1)")
l1.grid(row = 0, column = 0, pady = 2)
l2.grid(row = 0, column = 1, pady = 2)
l3.grid(row = 1, column = 0, pady = 2)
l4.grid(row = 1, column = 1, pady = 2)


When you run this code, a pop window will appear with a grid of (0, 0), (0, 1), (1, 0), and (1, 1) labels. Here is the output pop window:

Example 2

Let us create a widget that takes input from the user. In this example, we will expand on the previous example and put a widget against each column which will take input from the user. Refer to the code attached below.

As we have expanded on the previous example, we just added the entry() function to add the entry widget in the grid. The entry() function provided by the tkinter library is used to get the single line string input from the user. After the enter() function, we just defined the grid() values for the entry cells. When you execute the above code, you will get a pop window containing 4 cells filled with text and 4 cells with an entry tab. In each tab, you can enter a one-line string.

from tkinter import *
from tkinter.ttk import *
r = Tk()
l1 = Label(r, text ="First Entry")
l2 = Label(r, text ="Second Entry")
l3 = Label(r, text ="Third Entry")
l4 = Label(r, text ="Fourth Entry")
l1.grid(row = 0, column = 0, pady = 2)
l2.grid(row = 2, column = 0, pady = 2)
l3.grid(row = 0, column = 1, pady = 2)
l4.grid(row = 2, column = 1, pady = 2)
e1 = Entry(r)
e2 = Entry(r)
e3 = Entry(r)
e4 = Entry(r)
e1.grid(row = 1, column = 0, pady = 2)
e2.grid(row = 3, column = 0, pady = 2)
e3.grid(row = 1, column = 1, pady = 2)
e4.grid(row = 3, column = 1, pady = 2)


See the output widget of the grid below:

The entry() function accepts one line string containing any character; it can be a number, or it can be a symbol, or it can be a special character as well. See the output grid below:

Example 3

In the previous example, you can observe that we used a separate statement for creating each cell. For example, we needed a 2×2 sized grid, 4 cells in a grid, so we used 4 lines of code. What if we need a 100×100 sized grid? Will creating a 100×100 grid require 10,000 lines of code? Absolutely not. You can achieve the same result by using loops. It will not just reduce the lines of code but also increase the efficiency of the program reducing the probability of errors at the same time.

Here, we will use a nested for loop to make a grid of a defined size. We will define two for loops iterating through 10 rows and 10 columns to make a 10×10 sized grid. Let’s see the code below:

Here we defined a root() module to get a display environment, and then we added nested for loop for row and column, “for row in range(10)” and “for col in range(10)”. This will result in a 10×10 sized grid. The first loop will iterate from 0 to 9, and the second loop will also iterate from 0 to 9, making a 10×10 sized grid. For printing the index number for each cell, we used the text feature of label function(), “text = “Row%s – Col%s”. The “%s” is used to get the cell index from the loop.

import tkinter
r = tkinter.Tk()
for row in range(10):
   for col in range(10):
      tkinter.Label(r, text='Row%s - Col%s'%(row,col),
         borderwidth=1 ).grid(row=row,column=col)


Here is the 10×10 sized grid generated from the code given above:


In this article, we learned about the grid() function provided by the tkinter library. The grid function is used to create a square or rectangular-shaped grid in a display environment. We demonstrated some useful examples to help you understand how to use a grid function in a python program.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content