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

Options

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

Sr.No. Syntax & Description
1

-side side

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

2

-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.

3

-expand boolean

Used to make the widget the available space.

4

-padx number

Sets the padx for the widget.

5

-pady number

Sets the pady for the widget.

6

-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.

7

-after widgetName

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

A simple pack example for pack widget is shown below −

#!/usr/bin/wish

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
tk_layout_widgets.htm
Advertisements