GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python GUI
  • AVINASH NETHALA
  • 4 years ago4 years ago

This view

  • Original page
  • New address

GeezerVison is showing www.programminginpython.com — Layout Managers in Python GUI – Pack, Grid and Place - Programming In Python

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 Layout Manager
  • Grid Layout Manager
  • Place Layout Manager

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

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

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

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.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • More
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on WhatsApp (Opens in new window) WhatsApp

Online Python Compiler

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • More
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on WhatsApp (Opens in new window) WhatsApp

Leave a Reply Cancel reply

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

Related Posts

A simple python GUI calculator using TKInter

A simple python GUI calculator using TKInter

AVINASH NETHALA4 years ago4 years ago 0
Introduction to Python GUI – Labels, Frames, Buttons

Introduction to Python GUI – Labels, Frames, Buttons

AVINASH NETHALA4 years ago4 years ago 0
How to create a Domain to IP converter in Python GUI using TKInter

How to create a Domain to IP converter in Python GUI using TKInter

AVINASH NETHALA5 years ago4 years ago 0
How to create a temperature converter app in python GUI using TKInter

How to create a temperature converter app in python GUI using TKInter

AVINASH NETHALA6 years ago4 years ago 0

Recent Posts

  • Python vs R: Which Language is Better for Data Science?
  • Top 7 Python Books You Must Read in 2025
  • Best Python Web Frameworks: Django, Flask, and FastAPI Breakdown
  • 20 Best Python One-Liners to Boost Your Coding Skills
  • Matplotlib Data Visualization in Python

Suggested Courses

  • Complete Python Developer: Zero to Mastery.
  • Automate the boring stuff.
  • Python and Django Full Stack Web Developer Bootcamp.
  • Artificial Intelligence and Machine Learning E-Degree.
  • Complete Python & Python OOP.

Subscribe to our newsletter!

Great Deals

Ads
  • Health and Beauty
  • Deals on Electronics
  • Toys and Games
  • Sports and Outdoors
  • Pet Supplies
  • Shoes
  • Movies and TV

Categories

  • Algorithms (8)
  • Artificial Intelligence AI (3)
  • Basic Programs (25)
  • Books (1)
  • Data Structures (10)
  • Databases (1)
  • Dictionaries (1)
  • Files Handling (1)
  • Letter Patterns (26)
  • Lists (7)
  • Machine Learning (3)
  • Math Programs (6)
  • Pattern Programming (31)
  • Promoted (1)
  • Python for Automation (1)
  • Python for Data Science (6)
  • Python for Ethical Hacking (2)
  • Python For Finance (4)
  • Python GUI (5)
  • Python Libraries (6)
  • Python OOP (1)
  • Python Resources (3)
  • Python Tips and Tricks (10)
  • Searching Algorithms (2)
  • Sets (1)
  • Sorting Algorithms (6)
  • Tuples (1)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.