Difference between "grid" and "pack" geometry managers in Tkinter

TkinterPythonGUI-Programming

In order to view the widgets on the screen, we have to first associate each and every widget with the geometry manager. There are three ways in which we can view our widgets in the application. The Grid and Pack geometry manager are mostly used in many applications.

Pack Geometry Manager

The Pack geometry manager is one of the simplest geometry managers. We can use Pack manager to provide additional properties to the widgets such as padding, position respect to the X and Y axis, and expand property. It works on the basis of single row and single column. All the properties of Pack manager are applicable to all the widgets in Tkinter Library.

Grid Geometry Manager

Often, the Grid geometry manager is used for complex application development. It is manly used where we need to arrange many widgets inside a window such that they don't interfere with each other. Grid geometry manager works similar to the basic coordinate geometry system. It provides a layout feature to all the widget where we can place the widget in X and Y directionally. It works on the basis of multiple rows and multiple columns.

Example

# Import the required library
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win= Tk()

# Set the size of the window
win.geometry("700x350")

# Add Label and Button widgets
Button(win, text= "Button-1").grid(row=0, column=1, padx= 10, pady=20)
# Button(win, text= "Button-2").pack(padx= 10, pady=20)

win.mainloop()

Output

Now, uncomment the Button-2 widget and comment out the Button-1 widget to see the resultant output.

raja
Published on 07-Jun-2021 11:01:12
Advertisements