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 

Comments

Jack at 29 Apr, 2007 04:02
Is there any way to 'ungrid' something?
When I click a radio button I want one frame to be replaced with another preprepared frame.
Thanks
Reply to this.
dec at 10 Sep, 2007 08:04
$widget->gridForget;
Reply to this.
Anonymous at 06 Jul, 2007 11:24
When I run this Code on my Solaris Box which has 5.8.8 perl Insatlled I get a CoreDUMP.
Any Reason Why?

#!/usr/local/bin/perl
use Tk;
use strict;
# Main Window
my $mw = new MainWindow;
my $response = $mw -> messageBox(-message=>"Really quit?",
-type=>'yesno',-icon=>'question');

if( $response eq "yes" ) {
exit
}
else {
$mw -> messageBox(-type=>"ok",
-message=>"I know you like this application!");
}
MainLoop;

The Stack trace is given below:
$pstack core
core 'core' of 28384: /usr/local/bin/perl ./temp
ff1428c0 t_delete (43261c, ff1bc008, ff1c27cc, ff1c284c, ff1c2848, 0) + 68
ff141f50 _malloc_unlocked (68, 43261c, ff1bc008, 68, 43261c, 0) + 18c
ff141da8 malloc (68, 0, 20, 8, 215e4, fee9b938) + 20
feea624c _XEnq (0, ffbeebc4, 14e2c8, ffbeeb5c, 14e2c8, 20) + 28
fee9ccdc _XReply (14e2c8, 39, 0, 1, 39, 14e2c8) + 498
feea7088 XInternAtom (0, ff04b2fc, 0, 14e2c8, a5c46890, 40) + bc
fefd7474 Tk_InternAtom (38a4f0, ff04b2fc, fefcac28, ff031240, 1a8e0, 1) + 4c
ff0312b8 UpdateWmProtocols (3f67b0, 2c00, 153a, 1400, ff05c30c, 38a5e8) + e8
ff02addc TkWmMapWindow (38a4f0, ff067160, 0, 38a5e8, ff05c30c, 0) + 194
ff03703c Tk_MapWindow (38a4f0, 0, 12634, ff1fc910, ff20eda0, 38002) + 3c
fefed014 MapFrame (38a6d8, 32c8, 6f354, ff1f44f4, ff05c30c, 3000) + 68
ff1fde8c TclServiceIdle (0, ff1fed8c, 0, 1, 377d30, 388828) + 98
ff1fc910 Tcl_DoOneEvent (22, 0, 12634, ff3cdf18, ff20eda0, 0) + 1b0
fefecfe4 MapFrame (370770, 32c8, 6f354, ff1fd5b0, ff05c30c, 3000) + 38
ff1fde8c TclServiceIdle (0, ff1fed8c, 0, 0, 377d30, 36ab80) + 98
ff1fc910 Tcl_DoOneEvent (22, 0, 12634, fefb451c, ff20eda0, 0) + 1b0
fefe261c Tk_UpdateObjCmd (2e9408, 22a8f4, 2, ff07532c, 20, 32c8) + 90
fefba8b0 Call_Tk (13984c, 2, 226514, fefe258c, 1397a8, 1396f0) + 314
fefbd294 XSTkCommand (2585a8, 1, fefe258c, 2, 226514, 0) + 158
fefbd334 XStoTclCmd (3400, 32fc, 4, 226510, ff05c30c, 1398a0) + 90
00096a18 Perl_pp_entersub (1, 80, 2585a8, 139400, 226518, 247918) + 664
0008cfac Perl_runops_standard (24e230, 136c00, 132c00, 0, 0, 8d044) + 4c
00028dd8 S_run_body (0, ffbeef40, 8, 139800, 139800, 28ed58) + 164
00028acc perl_run (139a60, 139400, 139400, 139e90, 139e90, 137000) + 240
00024f6c main (2, ffbef4fc, 132c00, 139800, 136c00, 139800) + a4

The Tk Version installed is 804.027
Reply to this.
Niraj at 01 Sep, 2007 02:47
how to run a perl script in GUI
I have a code for transcribing a DNA into a RNA written in perl script but how should i get its output in GUI layout?
please reply soon.
Reply to this.
Anonymous at 09 Dec, 2007 09:03
My doubt is about how to implement reset in Perl tk.I have to refresh the values stored from the entry by using get() and again take the new values so can i use the same window for that and use refresh button??Reply me.
Reply to this.
Comment


Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed