Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Geometry

Status
Not open for further replies.

edpatterson

IS-IT--Management
Feb 24, 2005
186
I am using Active State Perl with the Tk Module

I am haviing a hard time grasping the placement of widgets. It seems they all either end up on top of each other, alligned doen the center or one of the sides.

I figured for practice I would create the simple layout below using lables, entry boxes and buttons:
[tt]
# +------------------------------------+
# | +-----------------------+ |
# | To:| | |
# | +-----------------------+ |
# | +-----------------------+ |
# | From:| | |
# | +-----------------------+ |
# | Message:+-----------------------+ |
# | | | |
# | | | |
# | | | |
# | | | |
# | | | |
# | | | |
# | | | |
# | | | |
# | | | |
# | +-----------------------+ |
# | +----+ +------+ |
# | |Send| |Cancel| |
# | +----+ +------+ |
# | |
# +------------------------------------+
[/tt]
I can create any of the objects, and I can even get the To: + entry box on one line and the cancel button below. But it goes haywire when I add other widgets.

Ed
 
A lot depends on what geometry manager you use. I've gotten used to pack although it is an aqcuired taste.

The thing to remember about pack is that if you pack, say, up, then it fills left-right, in the sense that you can't pack anything to the right or left of the thing you just packed. Likewise, if you pack left, it fills up-down.

What I would do to get the window you've described is to pack 2 frames left-right, inside another frame that's up-down:

pack [frame .f0] -side up
pack [frame .f0.f1 -borderwidth 4] -side left
pack [frame .f0.f2 -borderwidth 4] -side left


Now I'd use .f0.f1 for the labels and .f0.f2 for the widgets; at least the first two pairs

set w .f0.f1
pack [label $w.l1 -text To:] -side top
pack [label $w.l2 -text From:] -side top
set w .f0.f2
pack [entry $w.e1 -textvariable toVar] -side top
pack [entry $w.e2 -textvariable fromVar] -side top


Now I'll have .f1 to hold the message label and text:

pack [frame .f1] -side top
pack [label .f1.l1 -text Message:] -side left
pack [text .f1.t1 -height 12] -side left


But now, in order to get the buttons side by side, I'll put in another frame under .f2:

pack [frame .f2] -side top
pack [frame .f2.fb] -side top
set w .f2.fb
pack [button $w.b1 -text Send -command sendCmd] -side left
pack [button $w.b2 -text Cancel -command cnclCmd] -side left


or something like that

_________________
Bob Rashkin
 
Thanks, I'll give the frame approach then post the resluts.
 
Here is where I am so far:
[tt]
+--------MainWindow---------------+
| +-----------------------+ |
| |+----++---------------+| |
| || f1 || f2 ||<-f0|
| |+--- ++---------------+| |
| +-----------------------+ |
| +-----------------------+|
| | ||
|+------+| ||
|| f4 || f5 ||
|+------+| ||
| | ||
| +-----------------------+|
+---------------------------------+
[/tt]
I can't seem to align the f1 and f4 frames with the f2 and f5 frames.

Here is the actual code:
Code:
use strict;
use Tk;

# Frames
my $mw = MainWindow->new;
my $f0 = $mw->Frame(-borderwidth=>3,
					-relief=>'raised',
					-background=>'yellow')->pack(-side=>'top');
my $f1 = $f0->Frame(-borderwidth=>3,
					-relief=>'raised',
					-background=>'red')->pack(-side=>'left');
my $f2 = $f0->Frame(-borderwidth=>3,
					-relief=>'raised',
					-background=>'green')->pack(-side=>'left');
my $f3 = $mw->Frame(-borderwidth=>3,
					-relief=>'raised',
					-background=>'pink')->pack(-side=>'left');
my $f4 = $mw->Frame(-borderwidth=>3,
					-relief=>'raised',
					-background=>'orange')->pack(-side=>'left');
my $f5 = $mw->Frame(-borderwidth=>3,
					-relief=>'raised',
					-background=>'purple')->pack(-side=>'left');


# Labels
$f1->Label(-text=>'     To:', -anchor=>'e', -width=>6)->pack(-side=>'top');
$f1->Label(-text=>'   From:', -anchor=>'e', -width=>6)->pack(-side=>'top');
$f4->Label(-text=>'Message: ',-anchor=>'w', -width=>11)->pack(-side=>'left');

# Entry Boxes
my $eTo = $f2->Entry(-width=>40)->pack(-side=>'top');
my $eFrom = $f2->Entry(-width=>40)->pack(-side=>'top');

# Text Box
my $tMessage = $f5->Text(-width=>40,
						-height=>20)->pack(-side=>'top');

MainLoop;
 
ok. Maybe you should use the "place" geometry:
Code:
set w .
place [label $w.l1 -text To:] -x 50
place [entry $w.e1 -textvariable toVar] -x 100
place [label $w.l2 -text From:] -x 40 -y 20
place [entry $w.e2 -textvariable fromVar] -x 100 -y 20
place [label $w.l3 -text Message:] -x 20 -y 40
place [text $w.t1 -height 12] -x 100 -y 40
place [button $w.b1 -text Send -command sendCmd] -x 40 -y 220
place [button $w.b2 -text Cancel -command cnclCmd] -x 140 -y 220

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top