Previous
Dialogs 
Next
Common Widget Options 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Grid, Pack

Geometry Management : Grid, Pack

Grid

The grid command is used to communicate with the grid geometry manager that arranges widgets in rows and columns inside of another window, called the geometry master (or master window). The grid command can have any of several forms, depending on the option argument.
In short, grid is the name given to the thingy that will place your widget where you want it to be placed.

Some Options

-sticky STYLE This option may be used to position (or stretch) the widget within its cell. STYLE is a string that contains zero or more of the characters n, s, e or w. Each letter refers to a side (north, south, east, or west) that the slave will "stick" to. If both n and s (or e and w) are specified, the slave will be stretched to fill the entire height (or width) of its cavity.
-in MASTERThe widget will be placed in the MASTER widget.
-ipadx AMOUNT The AMOUNT specifies how much horizontal internal padding to leave on each side of the slave(s). This is space is added inside the slave(s) border.
-ipady AMOUNT The AMOUNT specifies how much vertical internal padding to leave on each side of the slave(s). Options same as -ipadx
-padx AMOUNT The amount specifies how much horizontal external padding to leave on each side of the slave(s), in screen units. AMOUNT may be a list of two values to specify padding for left and right separately.
-pady AMOUNT The amount specifies how much vertical external padding to leave on the top and bottom of the slave(s), in screen units. Options same as -padx.
-row N Insert the slave so that it occupies the Nth row in the grid. Row numbers start with 0. If this option is not supplied, then the slave is arranged on the same row as the previous slave specified on this call to grid, or the first unoccupied row if this is the first slave.
-column NInsert the slave so that it occupies the N'th column in the grid. Options same as -row
-rowspan NInsert the slave so that it occupies N rows in the grid. The default is one row.
-columnspan NInsert the slave so that it occupies N columns in the grid.

Example


frame .textarea
text .txt -width 20 -height 10 \
	 -yscrollcommand ".srl_y set" -xscrollcommand ".srl_x set"
scrollbar .srl_y -command ".txt yview" -orient v
scrollbar .srl_x -command ".txt xview" -orient h

grid .txt   -in .textarea -row 1 -column 1
grid .srl_y -in .textarea -row 1 -column 2 -sticky ns
grid .srl_x -in .textarea -row 2 -column 1 -sticky ew
grid .textarea -in . -row 1 -column 1

Lets take a closer look at the commands -
grid .txt -in .textarea -row 1 -column 1
This line will tell the interpreter to put the widget called '.txt' in the widget called '.textarea'(That is a frame, remember?). It will be put in the first column of the first row. The below digram will help you understand.

Column 1Column 2
Row 1 '.txt' widget will be here '.srl_y' widget's place
Row 2 '.srl_x' widget's position.

Using grid requires a bit of experience - but if you know HTML it would help a lot. The rows and columns are just like those in HTML tables. The concept is the same but the codes are very different.


pack

Pack is also a geometry manager like Grid - but much simpler. You don't have to specify the rows and columns as you did for grid. This is for you lazybones out there. Just put pack <widget_path> and the widget will be packed. If two widgets are given, the second widgets will be packed below the first(by default). But for more complex arrangements with pack one must use frames.

Some Options

-expand BOOLEANSpecifies whether the slaves should be expanded to consume extra space in their master. Sounds a lot like slaves eating up their masters, don't it? It just means that when the application is resized the widgets will automatically fill the space.
-fill STYLEThis will stretch the widget in x direction, y direction or both directions.
-in MASTERUse this if you want to pack your widget in a frame or something like that.
-side SIDESpecifies which side of the master the slave(s) will be packed against. Must be left, right, top, or bottom. Defaults to top.

Now for the example...


label .lab -text {DO YOU REMEMBER WHEN.......}
text .txt -yscrollcommand ".srl set"
.txt insert end {A Computer Was Something On TV From A Science Fiction Show
A Window Was Something You Hated To Clean....
And Ram Was The Cousin Of A Goat.....

Meg Was The Name Of My Girlfriend
And Gig Was Your Thumb Upright
Now They All Mean Different Things
And That Mega Bytes

An Application Was For Employment
A Program Was A TV Show
A Cursor Used Profanity
A Keyboard Was A Piano
 
Compress Was Something You Did To The Garbage
Not Something You Did To A File
And If You Unzipped Anything In Public
You'd Be In Jail For A While

Log On Was Adding Wood To The Fire
Hard Drive Was A Long Trip On The Road
A Mouse Pad Was Where A Mouse Lived
And A Backup Happened To Your Commode

Cut You Did With A Pocket Knife
Paste You Did With Glue
A Web Was A Spider's Home
And A Virus Was The Flu

I Guess I'll Stick To My Pad And Paper
And The Memory In My Head
I Hear Nobody's Been Killed In A Computer Crash
But, When It Happens They Wish They Were Dead
}
scrollbar .srl -command {.txt yview}

#The packing commands
pack .lab
pack .txt -expand 1 -fill both -side left
pack .srl -expand 1 -fill y
Previous
Dialogs 
Next
Common Widget Options 
blog comments powered by Disqus
Subscribe to Feed