Previous
Dialogs 
Next
Common Widget Options 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
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.
-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 => N Insert the slave so that it occupies the N'th column in the grid. Options same as -row
-rowspan => N Insert the slave so that it occupies N rows in the grid. The default is one row.
-columnspan => N Insert the slave so that it occupies N columns in the grid.

Example


#!/usr/local/bin/perl
use Tk;

# Main Window
my $mw = new MainWindow;
#Text Area
my $txt = $mw -> Text(-width=>40, -height=>10);
my $srl_y = $mw -> Scrollbar(-orient=>'v',-command=>[yview => $txt]);
my $srl_x = $mw -> Scrollbar(-orient=>'h',-command=>[xview => $txt]);
$txt -> configure(-yscrollcommand=>['set', $srl_y], 
		-xscrollcommand=>['set',$srl_x]);

#Geometry Management
$txt -> grid(-row=>1,-column=>1);
$srl_y -> grid(-row=>1,-column=>2,-sticky=>"ns");
$srl_x -> grid(-row=>2,-column=>1,-sticky=>"ew");

MainLoop;
Lets take a closer look at the commands -
$txt -> grid(-row=>1,-column=>1);
This line will tell the interpreter to put the widget called '$txt' in the first row of the first column of its parent widget. The below digram will help you understand.
Column 1Column 2
Row 1 '.$xt' 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 $widget -> pack; and the widget will be packed. 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.
-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...

#!/usr/local/bin/perl
use Tk;

# Main Window
my $mw = new MainWindow;
my $lab = $mw -> Label(-text=>"Do You Remember When...");
my $txt = $mw -> Text();
$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");

my $srl = $mw -> Scrollbar(-command=>[$txt,'yview']);
$txt -> configure(-yscrollcommand=>[$srl,'set']);

#The packing commands
$lab -> pack;
$txt -> pack(-expand => 1, -fill => "both", -side => "left");
$srl -> pack(-expand => 1, -fill => "y");

MainLoop;
Previous
Dialogs 
Next
Common Widget Options 
blog comments powered by Disqus
Subscribe to Feed