Tkinter Grid

In this tutorial, I will teach you about the Tkinter grid geometry manager. I will also discuss all the options and parameters that we can use with it and some relevant code examples for a better understanding. So, let’s get started.

The grid geometry manager is most flexible and easy to understand out of all three geometry managers(place and pack). It allows us to put widgets in the 2-dimensional table-like structure. It split the window into rows and columns and each cell holds the one widget.

Options

Here is the reference of all options that we can use with tkinter grid.

columnSet the column location to place the widget. default is 0.
rowSet the row location to place the widget. default is 0.
columnspanSet the number of columns widgets should occupy. default is 1.
rowspanSet the number of rows widget should occupy. default is 1.
ipadxSet the horizontal padding inside the border of the widget. default is 0.
ipadySet the vertical padding inside the border of the widget. default is 0.
padxSet the horizontal padding between widget and cell. default is 0.
padySet the vertical; padding between widget and cell. default is 0.
stickyUse this option to specify the expansion of the widget if the cell is greater than the widget. Use the combination of  SNE, and W. set the W+E+N+S to expand in all directions.

Tkinter Grid Examples

For a better understanding of the concepts. let’s look at some code examples.

Note: To fully understand these code example I assume you have some basic knowledge of the Tkinter label, button, and entry widgets.

Example 1

In this code example, we will use row and column options of the grid geometry manager to display a simple login form in the window.

from tkinter import *

window = Tk() # Create tkinter window

# Create labels
l1 = Label(window,text = "Username : ")
l2 = Label(window,text = "Password : ")

# Create entries
e1 = Entry(window)
e2 = Entry(window)

# Place elements into window using grid manager
l1.grid(row = 0,column = 0)
l2.grid(row = 1,column = 0)

e1.grid(row = 0,column = 1)
e2.grid(row = 1,column = 1)

window.mainloop()

Output :

Tkinter grid code example 1 output

Example 2

In the below code , we will add some padding between widgets and cells to make it look nice.

from tkinter import *

window = Tk() # Create tkinter window

# Create labels
l1 = Label(window,text = "Username : ")
l2 = Label(window,text = "Password : ")

# Create entries
e1 = Entry(window)
e2 = Entry(window)

# Adding Padding
l1.grid(row = 0,column = 0,padx = 10 , pady = 10)
l2.grid(row = 1,column = 0,padx = 10 , pady = 10)

e1.grid(row = 0,column = 1,padx = 10 , pady = 10)
e2.grid(row = 1,column = 1,padx = 10 , pady = 10)

window.mainloop()

Output :

Tkinter grid code example 2 output

Example 3

In this code example, we will use rowspan, columnspan and sticky options to create a more dynamic GUI design.

from tkinter import *

window = Tk()

# Create labels
l1 = Label(window, text = "Username")
l2 = Label(window, text = "Password")

# Create entry
e1 = Entry(window)
e2 = Entry(window)

# Create checkbutton
c1 = Checkbutton(window, text = "Remember Me")

# Load an png image
img = PhotoImage(file = r"dog.png")
img1 = img.subsample(9, 9)

# Set the png image to label
l3 = Label(window, image = img1)

# Create buttons
b1 = Button(window, text = "Forget")
b2 = Button(window, text = "Register")


l1.grid(row = 0, column = 0, sticky = W, pady = 5)
l2.grid(row = 1, column = 0, sticky = W, pady = 5)

e1.grid(row = 0, column = 1, pady = 5)
e2.grid(row = 1, column = 1, pady = 5)

c1.grid(row = 2, column = 0, sticky = W, columnspan = 2)

l3.grid(row = 0, column = 2,columnspan = 2, rowspan = 2, padx = 5, pady = 5)

b1.grid(row = 2, column = 2, sticky = E)
b2.grid(row = 2, column = 3, sticky = E)

window.mainloop()

Output :

Tkinter grid example 3 output

That’s the wrap for this tutorial. If you have any questions then please feel free to comment below. I will try my best to reply.

This Post Has 2 Comments

  1. Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.

Leave a Reply