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

Fellow Monks,
I have several Frames within a main window that I am trying to arrange. In my documentation I cannot seem to find the commands I need to perform this task. I am using pack() on the frames. Any assistance is much appreciated.

Lhamo_rin

Replies are listed 'Best First'.
Re: Arranging Frames in TK
by Tanalis (Curate) on Jun 26, 2003 at 12:09 UTC
    There are various ways to lay out frames in a window. What I tend to do is to use frames to contain sets of widgets, and then simply pack() the frames onto the mainwindow. This provides the default packing, which is to stack the frames one after the other from the top of the window to the bottom.

    If you'd like to do more than this, you can pack frames to one of the sides of the window. This is achieved as follows:

    my $mw = new MainWindow; my $frame = $mw->Frame(); # do something with the frame $frame->pack(-side => 'left');
    Here, side can take the values top, bottom, left or right, packing the frames to the respective side of the window. The default is 'top'.

    Moving up a step, you can change the order that frames are packed in using the $frame->pack(-after => $otherframe); syntax. after may be replaced with before to have the opposite effect.

    A multitude of other functionality for pack is available, without even mentioning the other layout managers. If you're likely to be using Tk a lot, I'd advise you get youself a good book on the subject (I'd recommend Learning Perl/Tk from O'Reilly).

    Hope that helps a little.
    -- Foxcub
    #include www.liquidfusion.org.uk

    Update: Typo correction (thanks herveus)

Re: Arranging Frames in TK
by herveus (Prior) on Jun 26, 2003 at 19:36 UTC
    Howdy!

    Whenever you ->pack a widget (and frames are just another widget that you happen to be able to put things into), the space gets divided into two pieces. One gets the widget and the other remains available for the next pack operation.

    If you pack(-side => 'top') or pack(-side => 'bottom'), the container is split by a horizontal line and you take whichever part you claim. 'left' and 'right' split with a vertical line.

    ASCII illustration follows:

    +----------------------+   +-----------+----------+
    | pack(-side => 'top') |   |           |          |
    |                      |   |  left     |   right  |
    +----------------------+   |           |          |
    |                      |   |           |          |
    |   bottom             |   |           |          |
    +----------------------+   +-----------+----------+

    Note that each pack further splits the space in two. You can not use pack to do:

    +----+---------+
    |    |         |
    |    +----+----+
    |    |    |    |
    +----+----+    |
    |         |    |
    +---------+----+

    If you need to do something like that, you have to use a different mechanism, and I don't know that well enough to speak off the top of my head.

    Another way to look at it is:

    You start with a rectangular space. You use pack to grab a rectangular chunk that touches three sides but not the fourth, into which you pack your widget. The remaining rectangular space is where the next pack goes. If the widget you pack is a container like a Frame, you have a new space to start with.

    "It's rectangles all the way down..."

    yours,
    Michael