The pack()
geometry manager organizes widgets in blocks before placing them in the parent widget. It uses the options fill
, expand
and side
.
Syntax
widget.pack(option)
Fill
Determines if the widget keeps the minimal space needed or takes up any extra space allocated to it. Attributes: NONE (default), X (fill horizontally), Y (fill vertically), or BOTH (fill both horizontally and vertically).
Expand
When set to YES, the widget expands to fill any space not used in widget's parent. Attributes: YES, NO.
Side
Determines which side of the widget's parent it packs to. Attributes: TOP (default), BOTTOM, LEFT, or RIGHT.
Example
from tkinter import *
root = Tk()
btn_fill = Button(root, text="Button")
btn_fill.pack(fill=X)
btn_expand = Button(root, text="Button")
btn_expand.pack(expand=YES)
btn_side = Button(root, text="Button")
btn_side.pack(side=RIGHT)
root.mainloop()
Result