Home » Layout Managers in Python GUI – Pack, Grid and Place

Layout Managers in Python GUI – Pack, Grid and Place

Layout Managers in Python GUI – Pack, Grid and Place

Hello everyone, welcome back to programminginpython.com. Here in this post, I am going to explain to you about different layout managers in Python GUI using TKInter package. In my previous posts, Temperature Converter app and Simple Calculator app where I created some Python GUI applications, I used either pack manager or grid manager. And there is one more layout manager called the place manager.

https://www.youtube.com/watch?v=lYQFIMOInSk”]

You can watch the video on YouTube here

However, in this post, I am going to cover all three, pack, grid, and place managers with a simple example for each one of them, and also will explain to you which layout manager to use based on different requirements.

Program on GitHub

Different Layout Managers in Python GUI – TKInter

Pack Manager

pack is one of the oldest and most used layout manager in Python’s TKInter package. It is very easy to style and place the widgets in an app using this pack() manager. When you use this pack() method on a widget, you don’t need to explicitly specify the position of that widget, pack automatically places the widget in the window based on the space available in the window.

You can use pack when your layout only consists of a group of items all aligned horizontally or vertically, mostly in the case of a navigation menu or something like that.

This pack() has 3 options to use they are: fill, expand, and side. I will create a simple example to demonstrate this pack manager.

I will use fill option to align three labels vertically.

# fill option
label1 = Label(root, text="Label 1", bg="#E74C3C", fg="white").pack(fill=X, padx=10)
label2 = Label(root, text="Label 2", bg="#2ECC71", fg="black").pack(fill=X, padx=10)
label3 = Label(root, text="Label 3", bg="#F1C40F", fg="white").pack(fill=X, padx=10)

Use side option to align them horizontally.

# side option
label4 = Label(root, text="Label 1", bg="#34495E", fg="white").pack(fill=X, padx=10, pady=10, side=LEFT)
label5 = Label(root, text="Label 2", bg="#5DADE2", fg="black").pack(fill=X, padx=10, side=LEFT)
label6 = Label(root, text="Label 3", bg="#A569BD", fg="white").pack(fill=X, padx=10, side=LEFT)

Expand operation to take up the full height until the content ends. We use listbox widget to fill up space.

# expand option
listbox = Listbox(root)
listbox.pack(fill=BOTH, expand=1)

for i in range(20):
    listbox.insert(END, str(i))

Pack Manager Example:

__author__ = 'Avinash'

from tkinter import *

root = Tk()
root.geometry('400x400+100+200')

# fill option
label1 = Label(root, text="Label 1", bg="#E74C3C", fg="white").pack(fill=X, padx=10)
label2 = Label(root, text="Label 2", bg="#2ECC71", fg="black").pack(fill=X, padx=10)
label3 = Label(root, text="Label 3", bg="#F1C40F", fg="white").pack(fill=X, padx=10)

# side option
label4 = Label(root, text="Label 1", bg="#34495E", fg="white").pack(fill=X, padx=10, pady=10, side=LEFT)
label5 = Label(root, text="Label 2", bg="#5DADE2", fg="black").pack(fill=X, padx=10, side=LEFT)
label6 = Label(root, text="Label 3", bg="#A569BD", fg="white").pack(fill=X, padx=10, side=LEFT)

# expand option
listbox = Listbox(root)
listbox.pack(fill=BOTH, expand=1)

for i in range(20):
    listbox.insert(END, str(i))

mainloop()

Program on Github

Layout Managers in Python GUI – Pack, Grid and Place
Layout Managers in Python GUI – Pack, Grid and Place

Grid Manager

grid is one of the most flexible layout manager out of the three GUI layout managers in Python. It was introduced as an alternative to pack. Grid allows you to position the elements in rows and columns which gives you more flexibility with your widgets.

I will create a simple 2×2 table structure using grid’s rows and column way.

__author__ = 'Avinash'

from tkinter import *

Label(text="Label 1", width=10).grid(row=0, column=0)
Label(text="Label 2", width=10).grid(row=0, column=1)

Label(text="Label 3", width=10).grid(row=1, column=0)
Label(text="Label 4", width=10).grid(row=1, column=1)

mainloop()

Program on GitHub

Layout Managers in Python GUI – Pack, Grid and Place
Layout Managers in Python GUI – Pack, Grid and Place

Place Manager

place is the most complex manager out of the 3 managers. It uses absolute positioning, when you choose place as your layout manager, then you need to specify the widgets positioning using x and y coordinates, When using this layout manager the size and position of the widgets do not change while resizing the window.

I will create a simple 4 labels which I will position based on x and y coordinates.

__author__ = 'Avinash'

from tkinter import *
root = Tk()

root.geometry('200x200+100+200')

Label(root, text="Label 1 : x=0, y=0", bg="#FFFF00", fg="black").place(x=0, y=0)
Label(root, text="Label 2 : x=50, y=40", bg="#3300CC", fg="white").place(x=50, y=40)
Label(root, text="Label 3 : x=75, y=80", bg="#FF0099", fg="black").place(x=75, y=80)
Label(root, text="Label 4 : x=25, y=100", bg="#00FFFF", fg="white").place(x=25, y=100)

mainloop()

Layout Managers in Python GUI – Pack, Grid and Place
Layout Managers in Python GUI – Pack, Grid and Place

Program on GitHub

That is all about Python GUI Layout managers pack, grid, and place.

Please feel free to look at my other posts on Python GUI programs.

Online Python Compiler

Leave a Reply

Your email address will not be published. Required fields are marked *