ozboomer has asked for the wisdom of the Perl Monks concerning the following question:

'lo everyone...

I'm fairly new to using Perl/Tk, although I've been using perl for quite a few years.

I would like to build a series of prompt labels and entry fields on a single window in a format like:

       Name: ____________________
    Version: __________
    Description: ________________________________________
         Date: __________

(I want the colons to line-up vertically and the whole thing to be centred in the window).

The code is included at the bottom of this post.

The use of pack() seems to be non-intuitive to me, once you get more than one widget in a frame.

If someone could explain what I'm doing wrong, I'd be very appreciative.

I'm using ActiveState Perl v5.6.1, build 628, Perl/Tk version 800.022 on Windows 98 SE (OEM).

Many thanks for any forthcoming suggestions.

John

use strict; use Tk; my $mw = MainWindow->new; # Main window $mw->geometry('532x432+100+200'); $mw->resizable(0, 0); # ...no re-sizing # ------------------------------------- # Create frames for widgets # my $hdrfrm = $mw->Frame( # The Top 'Bar' -background => 'magenta', -height => 10, )->pack(-side => 'top', -fill => 'x'); # , -pady => 5); my $entryfrm = $mw->Frame( # The Middle 'Bar' -background => 'cyan', -width => 200, -height => 200, )->pack(-side => 'top', -fill => 'x'); my $btnfrm = $mw->Frame( # The Button 'Bar' -background => 'darkgrey', # -height => 30, )->pack(-side => 'top', -fill => 'x'); # , -pady => 5); # ------------------------------------- # Place buttons # my $AddBtn = $btnfrm->Button( # 'Add' -text => ' Add ', -command => [$mw => 'destroy'] )->pack(-side => 'left', -padx => 10, -pady => 5); my $RptBtn = $btnfrm->Button( -text => 'Report', -command => [$mw => 'destroy'] )->pack(-side => 'left', -padx => 5, -pady => 5); my $AboutBtn = $btnfrm->Button( -text => 'About', -command => [$mw => 'destroy'] )->pack(-side => 'left', -padx => 100); my $DoneBtn = $btnfrm->Button( -text => ' Done ', -command => [$mw => 'destroy'] )->pack(-side => 'right', -padx => 10, -pady => 5); # ------------------------------------- # Place prompt labels and fields # my $pmt2 = $entryfrm->Label( # Name -text => 'Name:', -background => 'magenta', )->pack(-side => 'left'); # , -anchor => 'n'); my $fld2 = $entryfrm->Entry( -relief => 'sunken' )->pack(-side => 'left'); # , -anchor => 'nw'); my $pmt3 = $entryfrm->Label( # Version -text => 'Version:', -background => 'magenta', )->pack(-side => 'left'); my $fld3 = $entryfrm->Entry( -relief => 'sunken' )->pack(-side => 'left'); my $pmt4 = $entryfrm->Label( # Description -text => 'Description:', -background => 'magenta', )->pack(-side => 'top'); my $fld4 = $entryfrm->Entry( -relief => 'sunken' )->pack(-side => 'top'); MainLoop;

Replies are listed 'Best First'.
Re: Perl/Tk: Need Assistance with use of pack()
by matija (Priest) on Apr 25, 2004 at 11:02 UTC
    In order to get the colons to line up verticaly, you will need to create an invisible frame containing JUST the labels, and make all the labels right-justified.

    Then create another frame for the entry fields and place it to the right of that frame (all of that in the major frame).

    Center the major frame on the screen, and you're done. In general, You should create frames from the top down (hierarchicaly, not positionaly speaking), and when you want something to line up, you should put that on the edge of a frame.

      Some good clues there... I've created an 'all-encompassing' frame in addition to the 'field' and 'prompt' frames, and have changed the physical order of when the frames are created and that seems to work better - it actually appears more like there's a hierarchy from 'out to in' on the frames and how pack() deals with them.

      I've also used -anchor to position the frames and Entry items within the frames and things seem to make a bit more sense now (to both Perl AND me!).

      The current problem I'm having now is to work-out why text in a Label is always displayed centred when -justify => 'right' is included in a construct like:-

      my $mw = MainWindow->new; my $pmt3 = $mw->Label( -width => 10, # -height => 3, -justify => 'right', # -text => 'Version: more text is it longer enough', -text => 'Version:', -background => 'magenta', )->pack; MainLoop;
      I've tried looking through a couple of FAQs, tried searching on the web, looking in a couple of my books, etc. but it just doesn't seem to want to work! I'll nut about on it for a while before I post a question on it though.

      Many thanks for your suggestions -- they were very helpful :)

      John

        I suggest a healty reading of Tk::pack (fyi, it is not the only geometry manager)
        use Tk; use strict; use warnings; my $mw = MainWindow->new( -bg => 'beige', ); my $l = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#EEE000', )->pack( -fill => 'x', -side => 'left', -anchor => 'n', ); my $r = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#F0F000', )->pack( -fill => 'both', -side => 'left', -anchor => 'n', ); my $r2 = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#F0F000', )->pack( -fill => 'both', -side => 'left', -anchor => 'n', ); for( qw[ Name: Version: Description: Date: ] ){ $l->Label( -text => $_, # -justify => 'right', -anchor => 'e', )->pack( #-anchor => 'e', # if you don't fill -fill => 'x', ); $r->Label( -text => $_, # -justify => 'right', -anchor => 'e', )->pack( -anchor => 'e', # if you don't fill #-fill => 'x', ); $r2->Label( -text => $_, -justify => 'right', # -anchor => 'e', )->pack( #-anchor => 'e', # if you don't fill #-fill => 'x', ); } #$top->Label(-text => "Enter the scroll frame")->pack; MainLoop;

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

•Re: Perl/Tk: Need Assistance with use of pack()
by merlyn (Sage) on Apr 25, 2004 at 16:29 UTC
    This really looks like a grid to me, not a pack. Quickly constructed test:
    use Tk; my $mw = MainWindow->new; my @tags = qw(name version description date); for my $row (0..3) { $mw->Label(-text => $tags[$row])->grid ( -column => 0, -row => $row, -sticky => 'e', ); $mw->Entry()->grid ( -column => 1, -row => $row, -sticky => 'w', ); } MainLoop;
    When you say your experiment with grid "hangs", you might be trying to use both pack and grid in the same container. Don't! If this needs to exist within a larger framework, wrap it all in a Frame, and then pack the frame in the mainwindow.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Good day, sensei...

      As always, it proves there is more than one way to do things, especially in Perl :)

      Yes, I was trying to use grid and pack together on different widgets within the same frame... but I think that was in a more-complex incarnation of my investigations.

      Thanks for your help.

      John