Python | grid() method in Tkinter
Last Updated :
21 Aug, 2024
The Grid geometry manager puts the widgets in a 2-dimensional table. The master widget is split into a number of rows and columns, and each “cell” in the resulting table can hold a widget. The grid manager is the most flexible of the geometry managers in Tkinter. If you don’t want to learn how and when to use all three managers, you should at least make sure to learn this one. Consider the following example –
Creating this layout using the pack manager is possible, but it takes a number of extra frame widgets, and a lot of work to make things look good. If you use the grid manager instead, you only need one call per widget to get everything laid out properly. Using the grid manager is easy. Just create the widgets, and use the grid method to tell the manager in which row and column to place them. You don’t have to specify the size of the grid beforehand; the manager automatically determines that from the widgets in it.
Code #1:
Python
# import tkinter module
from tkinter import * from tkinter.ttk import *
# creating main tkinter window/toplevel
master = Tk()
# this will create a label widget
l1 = Label(master, text = "First:")
l2 = Label(master, text = "Second:")
# grid method to arrange labels in respective
# rows and columns as specified
l1.grid(row = 0, column = 0, sticky = W, pady = 2)
l2.grid(row = 1, column = 0, sticky = W, pady = 2)
# entry widgets, used to take entry from user
e1 = Entry(master)
e2 = Entry(master)
# this will arrange entry widgets
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)
# infinite loop which can be terminated by keyboard
# or mouse interrupt
mainloop()
Output:
Code #2: Creating the layout which is shown above.
Python
# import tkinter module
from tkinter import * from tkinter.ttk import *
# creating main tkinter window/toplevel
master = Tk()
# this will create a label widget
l1 = Label(master, text = "Height")
l2 = Label(master, text = "Width")
# grid method to arrange labels in respective
# rows and columns as specified
l1.grid(row = 0, column = 0, sticky = W, pady = 2)
l2.grid(row = 1, column = 0, sticky = W, pady = 2)
# entry widgets, used to take entry from user
e1 = Entry(master)
e2 = Entry(master)
# this will arrange entry widgets
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)
# checkbutton widget
c1 = Checkbutton(master, text = "Preserve")
c1.grid(row = 2, column = 0, sticky = W, columnspan = 2)
# adding image (remember image should be PNG and not JPG)
img = PhotoImage(file = r"C:\Users\Admin\Pictures\capture1.png")
img1 = img.subsample(2, 2)
# setting image with the help of label
Label(master, image = img1).grid(row = 0, column = 2,
columnspan = 2, rowspan = 2, padx = 5, pady = 5)
# button widget
b1 = Button(master, text = "Zoom in")
b2 = Button(master, text = "Zoom out")
# arranging button widgets
b1.grid(row = 2, column = 2, sticky = E)
b2.grid(row = 2, column = 3, sticky = E)
# infinite loop which can be terminated
# by keyboard or mouse interrupt
mainloop()
Output:
Warning: Never mix grid() and pack() in the same master window.
Python | grid() method in Tkinter – FAQs
What is the Python Tkinter place() method?
The place() method in Tkinter is a geometry manager that allows you to explicitly set the position and size of a widget within a window or a frame by specifying the exact coordinates. It can be useful for precise widget placement, especially in applications where layout needs to be tightly controlled.
Example:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Hello, World!")
label.place(x=50, y=50) # Placing the label at coordinates x=50, y=50
root.mainloop()
How does the grid() function put the widget on the screen?
The grid() function in Tkinter places widgets in a grid layout where you define rows and columns. Each widget can be assigned to a specific row and column, and you can also specify how many rows or columns a widget should span.
Example:
import tkinter as tk
root = tk.Tk()
label1 = tk.Label(root, text="Label 1")
label1.grid(row=0, column=0) # Place Label 1 at row 0, column 0
label2 = tk.Label(root, text="Label 2")
label2.grid(row=1, column=1) # Place Label 2 at row 1, column 1
root.mainloop()
How is the grid() geometry management method used in Tkinter?
The grid() method is used for placing widgets in a two-dimensional grid. It provides the flexibility to specify the exact row and column for each widget, the number of columns or rows that the widget should span, and how the widget should expand to fill the space if the window is resized.
Example:
import tkinter as tk
root = tk.Tk()
for i in range(5):
for j in range(5):
label = tk.Label(root, text=f'Row {i} Col {j}')
label.grid(row=i, column=j) # Each label is placed in its corresponding grid position
root.mainloop()
What is padx and pady?
padx and pady are optional arguments used with geometry management methods like pack(), grid(), and place() in Tkinter. They define the amount of padding along the x-axis (padx) and y-axis (pady) around the widget.
padx provides horizontal padding.pady provides vertical padding.
This padding adds extra space around the widget, either inside (internal padding) or outside (external padding) the widget’s border.
Example:
label.grid(row=0, column=0, padx=10, pady=20)
What is the difference between pack and grid in Tkinter?
- pack() is a geometry manager that organizes widgets in a block, which means it places them one after the other in the specified order and direction (top to bottom or left to right). It is simpler to use but less versatile when complex layouts are required.
- grid() places widgets in a grid layout where each widget is positioned using row and column indices. This method is more flexible and suitable for creating complex layouts as it allows precise placement of widgets in relation to each other.
Key Differences:
pack() is easier for simple layouts and stacking widgets.grid() is better for creating complex, table-like layouts where precise positioning is necessary.- Widgets managed by
pack() and grid() cannot be mixed in the same master window or frame.
These geometry management methods provide various options for designing user interfaces in Tkinter, each with specific use cases depending on the complexity and requirements of the layout