Here we are providing basic tutorial on GridLayout in Tkinter

First consider the code given below.

Code

from tkinter import *

root = Tk()

label_1 = Label(root, text="Name")
label_2 = Label(root, text="Password")
entry_1 = Entry(root)
entry_2 = Entry(root)

label_1.grid(row=0)
label_2.grid(row=1)

entry_1.grid(row=0, column=1)
entry_2.grid(row=1, column=1)

root.mainloop()


Output

output


Explanation of above code

Below section will give an explanation of the above Tkinter program. If you want to start from the beginning, please visit our Introduction to GUI tutorial.

Entry

This can be assumed as a way to create an input field just like in form. In above code, two entries i.e input fields are there in the root window , one for label Name and one for label Password. An entry can be created using Entry pre-defined function in Tkinter.

GridLayout

It is a way of distributing the layout in grids. This is done using grid pre-defined function in Tkinter. For example, instead of using pack() above, grid is being used. For label Name, grid is set for 0th row in the window, column is set to 0 by default. So, Name comes in 0th row and 0th column of the window. Similarly, input field for Name is in 0th row and 1st column. Label Password is in 1st row and 0th column. Input field for Password is in 1st row and 1st column.