Layout Management
Layout Management
Creating widgets is only part of GUI design.
You also need to control where widgets appear.
This is called layout management.
Tkinter provides tools to position widgets inside a window.
The three main layout managers are:
pack()grid()place()
Why Layout Matters
Good layout helps you:
- organize widgets clearly
- build professional interfaces
- improve usability
- control spacing and alignment
1. pack()
pack() places widgets automatically.
It is the easiest layout manager for beginners.
Widgets are stacked top to bottom by default.
Basic Example
import tkinter as tk
window = tk.Tk()
tk.Label(window, text="Title").pack()
tk.Button(window, text="Start").pack()
tk.Button(window, text="Exit").pack()
window.mainloop()
Pack Options
Side
button.pack(side="left")
Values:
- left
- right
- top
- bottom
Padding
button.pack(pady=10, padx=20)
Adds space around widgets.
2. grid()
grid() places widgets in rows and columns.
Great for forms and calculators.
Basic Example
import tkinter as tk
window = tk.Tk()
tk.Label(window, text="Name").grid(row=0, column=0)
tk.Entry(window).grid(row=0, column=1)
tk.Label(window, text="Age").grid(row=1, column=0)
tk.Entry(window).grid(row=1, column=1)
window.mainloop()
Why grid() Is Useful
Perfect for:
- login forms
- registration forms
- tables
- calculator buttons
Grid Padding
widget.grid(row=0, column=0, padx=5, pady=5)
Important Rule
Do not mix pack() and grid() in the same parent window.
Use one layout system per container.
3. place()
place() uses exact x/y positions.
Example
import tkinter as tk
window = tk.Tk()
window.geometry("300x200")
tk.Button(window, text="OK").place(x=100, y=50)
window.mainloop()
Why place() Is Less Common
It can break when window sizes change.
Usually pack() or grid() is better.
Which One Should You Use?
Use pack()
For simple vertical layouts.
Use grid()
For forms and structured layouts.
Use place()
For special fixed-position designs.
Full Example: Login Form
import tkinter as tk
window = tk.Tk()
tk.Label(window, text="Username").grid(row=0, column=0)
tk.Entry(window).grid(row=0, column=1)
tk.Label(window, text="Password").grid(row=1, column=0)
tk.Entry(window, show="*").grid(row=1, column=1)
tk.Button(window, text="Login").grid(row=2, column=0, columnspan=2)
window.mainloop()
Common Beginner Errors
Mixing pack() and grid()
Use one layout manager in the same container.
No Padding
Widgets look cramped.
Wrong Row/Column Numbers
Check positions carefully.
Code Along
Build a form using grid():
- Name label + entry
- Email label + entry
- Submit button
Mini Challenge
Create a calculator layout using grid():
7 8 9
4 5 6
1 2 3
0 +
Use buttons only.
Real World Use Case
All desktop apps need layout systems for forms, dashboards, menus, and tools.
Quiz
- What is layout management?
- Which layout manager uses rows and columns?
- Which one uses exact positions?
- Why is
grid()great for forms?
Assignment
Build a login form using labels, entries, and buttons with grid().
Summary
You learned how to position widgets using pack(), grid(), and place() in Tkinter.