Pack Layout Manager
Pack Layout Manager
The pack arranges widgets around the edges of their container. The container can be
the root window or a frame.
The following shows how to use the pack geometry manager to arrange two Label
widgets on the root window:
import tkinter as tk
root = tk.Tk()
root.title('Pack Demo')
root.geometry("350x200")
# box 1
box1 = tk.Label(root, text="Box 1", bg="green", fg="white")
box1.pack()
# box 2
box2 = tk.Label(root, text="Box 2", bg="red", fg="white")
box2.pack()
root.mainloop()
In this example, we have two boxes: green and red. The pack() places the boxes on top
of each other because it uses the default options.
Before diving into other options, you need to understand the x and y coordinates of the
window:
The top left corner of the window is the origin with the coordinate (0,0). The
x-coordinate increments from left to right and the y-coordinate increments from top to
bottom.
To create internal paddings for widgets, you use the ipadx and ipady parameters:
● ipadx creates padding left and right, or padding along the x-axis.
● ipady creates padding top and bottom, or padding along the y-axis.
For example, the following program uses ipadx and ipady parameters to set the
internal paddings of each box:
import tkinter as tk
root = tk.Tk()
root.title('Pack Demo')
root.geometry("350x200")
# box 1
box1.pack(ipadx=10, ipady=10)
# box 2
box2.pack(ipadx=10, ipady=10)
root.mainloop()
Output:
3) Using the fill option
The fill option accepts one of three string constants defined in the tkinter module.
Note that if you import the tkinter module as tk, you need to reference the constants
X, Y, and BOTH using the tk prefix e.g., tk.X, tk.Y, and tk.BOTH.
The following example uses the fill parameter to set the fill option for the first label:
box1.pack(ipadx=20,ipady=20,fill=tk.Y)
… you’ll see that the first widget doesn’t fill all spaces vertically:
This is because the pack allocates space to each widget as highlighted in the following
picture:
When you use the fill option, the area each widget can fill is constrained by their
allocated areas.
The expand option allocates more available space to a widget. If you add the expand
option to the first widget:
box1.pack(ipadx=20,ipady=20,expand=True)
In this example, the first widget takes all available space in the window except for the
space allocated to the second widget.
Since the first widget doesn’t have the fill option, it floats in the middle of the
allocated area.
…you’ll see that the first widget fills up all of its allocated space:
If you add the expand option to both widgets, the pack manager will allocate the space
to them evenly. For example:
import tkinter as tk
root = tk.Tk()
root.title('Pack Demo')
root.geometry("350x200")
# box 1
# box 2
root.mainloop()
Output:
Notice that the second widget doesn’t use all allocated space because it doesn’t have
the fill option.
When you set the expand to True for all widgets, the pack geometry manager will
allocate spaces to them evenly. However, this is only true when all the widgets share the
same side.
The anchor allows you to anchor the widget to the edge of the allocated space. It
accepts one of the following values:
Sticky Description
For example, the following program shows widgets that use E and W anchors:
import tkinter as tk
root = tk.Tk()
root.title('Pack Demo')
root.geometry("350x200")
# box 1
root.mainloop()
Output:
● tkinter.TOP
● tkinter.LEFT
● tkinter.RIGHT
● tkinter.BOTTOM
The side defaults to TOP. In other words, widgets are aligned to the top of their
container.
The following example sets the side for the first widget to tkinter.LEFT :
import tkinter as tk
root = tk.Tk()
root.title('Pack Demo')
root.geometry("350x200")
# box 1
# box 2
root.mainloop()
Output:
In this example, the expand option may not work as you expected. The reason is that
widgets have different sides.
For example:
import tkinter as tk
root = tk.Tk()
root.title('Pack Demo')
root.geometry("350x200")
# box 1
# box 2
root.mainloop()
Output:
To set the external paddings of widgets, you use the padx and pady parameters:
For example:
import tkinter as tk
root = tk.Tk()
root.title('Pack Demo')
root.geometry("350x200")
# box 1
fill=tk.BOTH, expand=True)
# box 2
fill=tk.BOTH, expand=True)
root.mainloop()
Output:
import tkinter as tk
root = tk.Tk()
root.title('Pack Demo')
root.geometry("300x200")
label1.pack(**ipadding, fill=tk.X)
label2.pack(**ipadding, fill=tk.X)
label3.pack(**ipadding, fill=tk.X)
root.mainloop()
Output:
import tkinter as tk
root = tk.Tk()
root.title('Login')
root.geometry("350x220")
fields = {}
fields['username_label'] = ttk.Label(text='Username:')
fields['username'] = ttk.Entry()
fields['password_label'] = ttk.Label(text='Password:')
fields['password'] = ttk.Entry(show="*")
root.mainloop()
Output:
How it works.
fields = {}
fields['username_label'] = ttk.Label(text='Username:')
fields['username'] = ttk.Entry()
fields['password_label'] = ttk.Label(text='Password:')
fields['password'] = ttk.Entry(show="*")
Summary
● Use Tkinter pack geometry manager to arrange widgets in a top-down layout or
side by side.
● Use the fill, expand, and side options of pack geometry manager to control
how widgets are arranged.