Tk - Pack Widget


The pack widget is a rectangular container widget that groups widgets for designing GUI. The syntax for pack widget is shown below.

pack packName options


The options available for the pack widget are listed below in the following table −

Sr.No. Syntax & Description

-side side

Packs the widget to given side of the parent window. It can be top, bottom, left, and right. The default is top.


-anchor edge

Pack widget will be anchored to specific side if the width is less than space is assigned. The valid edges are n, e, w, and s.


-expand boolean

Used to make the widget the available space.


-padx number

Sets the padx for the widget.


-pady number

Sets the pady for the widget.


-fill direction

Widget may expand to fill extra space in its parcel. The default is none. The direction may be none, x to fill vertically,y to fill horizontally, and both to fill both ways.


-after widgetName

Pack this widget after widgetName, generally on top of it.

A simple pack example for pack widget is shown below −


label .label1 -background green -text "Hello World1" -width 30
label .label2 -background gray -text "Hello World2" 
frame .myFrame2 -background blue  -relief ridge -borderwidth 8 -padx 10 -pady 10
   -height 100 -width 50
pack .label1 -side top -anchor s
pack .label2 -side top -anchor s 
pack .myFrame2 -padx 10 -fill x -side bottom -anchor n -after .label2

When we run the above program, we will get the following output −

Pack Example