Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Tk - aligning Labels

by packetstormer (Monk)
on Sep 13, 2012 at 14:12 UTC ( #993500=perlquestion: print w/ replies, xml ) Need Help??
packetstormer has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I am using Tk for the first time and I can't seem to align the label/entry correctly vertically. Has anyone done this beofore? I did try padding out the text but it looks terrible

#!/usr/bin/perl use strict; use Tk; my $btypes; my $locations; my $bstats; my $mw; $mw = MainWindow->new; $mw->geometry("400x300"); $mw->title("First App"); my $frmTop = $mw->Frame(-bd => 2,-relief => 'ridge')->pack(-side => 't +op', -fill => 'x', -pady => 3); # Location label/entry my $frmLocations = $frmTop->Frame(-bd => 2)->pack(-side => 'top', -fil +l => 'y'); my $lblLocationsID = $frmLocations->Label(-text => "Locations")->pack( +-side => 'left'); my $edtLocationsID = $frmLocations->Entry(-textvariable => \$locations +,-background => 'white')->pack(-side => 'left'); # type label/entry my $frmLocations = $frmTop->Frame(-bd => 2)->pack(-side => 'top', -fil +l => 'y'); my $lblBtypeID = $frmLocations->Label(-text => "Types" )->pack(-sid +e => 'left'); my $edtBtypeID = $frmLocations->Entry(-textvariable => \$btypes,-backg +round => 'white')->pack(-side => 'left'); MainLoop;

Thanks!

EDIT: Not sure about the $frmLocations being overwritten in the "type lable/entry" !

Comment on Tk - aligning Labels
Download Code
Re: Tk - aligning Labels
by sundialsvc4 (Abbot) on Sep 13, 2012 at 15:33 UTC

    You’d have to use a system of nested frames ...

    ... and, to be quite honest with you, I’m not sure that I would actually bother.   Especially if you consider how the window should look if it is resized by the user, it just might be “good enough” if the entry fields do not precisely line-up vertically.   I used to lose a lot of sleep over issues like that; I don’t anymore.

Re: Tk - aligning Labels
by thundergnat (Deacon) on Sep 13, 2012 at 16:05 UTC

    Maybe use a different geometry manager? grid perhaps? Just remember to not mix geometry managers in a single container widget. e.g. Below, the stuff in $mw is 'pack'ed but the stuff in $frmTop is 'grid'ed.

    UPDATE: added -sticky attributes.

    #!/usr/bin/perl use strict; use Tk; my $btypes; my $locations; my $bstats; my $mw; $mw = MainWindow->new; $mw->geometry("400x300"); $mw->title("First App"); my $frmTop = $mw->Frame(-bd => 2,-relief => 'ridge')->pack(-side => 't +op', -fill => 'x', -pady => 3); # Location label/entry my $lblLocationsID = $frmTop->Label(-text => "Locations") ->grid(-row => 0, -column => 0, -sticky => 'w'); my $edtLocationsID = $frmTop->Entry(-textvariable => \$locations,-back +ground => 'white') ->grid(-row => 0, -column => 1); # type label/entry my $lblBtypeID = $frmTop->Label(-text => "Types") ->grid(-row => 1, -column => 0, -sticky => 'w'); my $edtBtypeID = $frmTop->Entry(-textvariable => \$btypes, -background + => 'white') ->grid(-row => 1, -column => 1); MainLoop;
Re: Tk - aligning Labels
by stefbv (Deacon) on Sep 13, 2012 at 16:46 UTC

    Another option is to use the 'form' geometry manager:

    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::widgets qw(LabFrame); my $mw = MainWindow->new; $mw->title('First App'); #--- Locations my $locations_f = $mw->LabFrame( -label => 'Locations', -labelside => 'acrosstop', -foreground => 'blue', ); $locations_f->pack( -side => 'top', -expand => 1, -fill => 'x', ); #-- locations my $llocations = $locations_f->Label( -text => 'Locations' ); $llocations->form( -top => [ %0, 0 ], -left => [ %0, 5 ], ); my $elocations = $locations_f->Entry( -width => 20, ); $elocations->form( -top => [ %0, 0 ], -left => [ %0, 80 ], -padright => 5, ); #-- types my $ltypes = $locations_f->Label( -text => 'Types' ); $ltypes->form( -top => [ $llocations, 8 ], -left => [ %0, 5 ], ); my $etypes = $locations_f->Entry( -width => 15, ); $etypes->form( -top => [ '&', $ltypes, 0 ], -left => [ %0, 80 ], -padbottom => 5, ); MainLoop;

    Regards, Ştefan

Re: Tk - aligning Labels
by Khen1950fx (Canon) on Sep 13, 2012 at 17:45 UTC
    I used windowCreate:
    #!/usr/bin/perl use strict; use warnings; use Tk; my $btypes; my $locations; my $bstats; my $mw = MainWindow->new; $mw->geometry("400x300"); $mw->title("First App"); my $f = $mw->Frame->pack(-side => 'bottom'); $f->Button( -text => "Exit", -command => sub { exit; } )->pack(-side => 'left'); my $t = $mw->Scrolled("Text", -width => 50, -wrap => 'none' )->pack(-expand => 1, -fill => 'both'); foreach (qw/Locations Types/) { $mw = $t->Label( -text => "$_:", -relief => 'groove', -width => 30, ); $t->windowCreate('end', -window => $mw); $mw = $t->Entry( -width => 20, -textvariable => \$locations, ); $t->windowCreate('end', -window => $mw); $t->insert('end', "\n"); }; $t->configure(-state => 'enabled'); MainLoop;
Re: Tk - aligning Labels
by Sandy (Curate) on Sep 14, 2012 at 12:05 UTC
    This is actually VERY easy to do, if you use some of the options to LABEL.

    Add ,-width=>15,-anchor=>'w' to your Label definitions and it will look much better.

    Modified code:

    #!/usr/bin/perl use strict; use Tk; my $btypes; my $locations; my $bstats; my $mw; $mw = MainWindow->new; $mw->geometry("400x300"); $mw->title("First App"); my $frmTop = $mw->Frame(-bd => 2,-relief => 'ridge')->pack(-side => 't +op', -fill => 'x', -pady => 3); # Location label/entry my $frmLocations = $frmTop->Frame(-bd => 2)->pack(-side => 'top', -fil +l => 'y'); my $lblLocationsID = $frmLocations->Label(-text => "Locations",-width= +>15,-anchor=>'w')->pack(-side => 'left'); my $edtLocationsID = $frmLocations->Entry(-textvariable => \$locations +,-background => 'white')->pack(-side => 'left'); # type label/entry my $frmLocations = $frmTop->Frame(-bd => 2)->pack(-side => 'top', -fil +l => 'y'); my $lblBtypeID = $frmLocations->Label(-text => "Types",-width=>15, -an +chor=>'w' )->pack(-side => 'left'); my $edtBtypeID = $frmLocations->Entry(-textvariable => \$btypes,-backg +round => 'white')->pack(-side => 'left'); MainLoop;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993500]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-12-28 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls