Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Tk: pack, grid or place?

by davies (Parson)
on May 13, 2005 at 08:32 UTC ( #456624=perlquestion: print w/replies, xml ) Need Help??
davies has asked for the wisdom of the Perl Monks concerning the following question:

Are there strong reasons for using certain geometry managers? I ask because I have only just started with perl, and I find place by far the easiest to get right. However, it appears last - if at all - in all the docs and tuts I have read, and in the code I have seen, pack is by far the most popular, while I have not seen a single use of place. Since all these people know far more about perl than I do, I'm scared that using place will lay traps that I will fall into later.

TIA & regards,

John Davies

Update: jdporter pointed out in reply to another node I wrote that place can prevent scrollbars from working.

Replies are listed 'Best First'.
Re: Tk: pack, grid or place?
by davidj (Priest) on May 13, 2005 at 09:31 UTC
    Hey davies,

    Here's my take on Tk geometry:
    pack is the most popular because its the easiest to use. This ease of use is ideal for applications that require simple layouts. Consequently, tweaking widget layout can be quite burdensome using pack. If what you are developing has a simple layout, by all means, go with pack.

    place uses the cartesian coordinate system for locating widgets. This makes coding widget layout more burdensome because you have to code the coordinates. If you have a lot of widgets, this can become quite tedious. However, it gives you much better layout control. You can tweak to your hearts delight and you can do things with place that are extremely difficult to do with pack. Also, with pack, the final layout depends upon the order in which the widgets are packed. This is not true with place. While not generally a problem, I have run into it sometimes.

    my personal favorite is the form geometry manager. Instead of using cartesian coordinates, widget placement is indicated by percentages. This is easier for some people to conceptualize. Also, a great advantage of form is that placing widgets relative to other widgets is extremely easy. When I code gui apps, I am a freak for widget placement control and form is much better than pack or place in this regard. Give it a try sometime.

    Also, don't really worry about getting caught in any traps by using a particular layout manager. pack, place, grid, and form are all supported, so its not like someone will run your app and have it fail because of the manager you decide to use. The only things you really need to consider are 1) how much control do you want over layout and 2) how is the code going to be maintained. Then, choose your layout manager accordingly.

Re: Tk: pack, grid or place?
by g0n (Priest) on May 13, 2005 at 09:13 UTC
    FWIW: when I first started using Tk, I found pack really easy to use. As time wore on and I started doing more complex things that required a greater degree of control over the exact placement of widgets within a form, I started to find pack very limited, and moved to mostly using grid, and only using pack for quick little things. I don't think I ever used place.

    The issue is, when you come to do ammend your app in future, will the geometry manager you have used give you sufficient control over placement? If you're happy that place will, I see no reason not to carry on using it.

    Update: AFAIK, the only thing that is likely to bite you is the varying behaviours of the geometry managers when you resize a window. As long as you know what to expect, it shouldn't be a problem.


    g0n, backpropagated monk

Re: Tk: pack, grid or place?
by PodMaster (Abbot) on May 13, 2005 at 09:04 UTC
    Since all these people know far more about perl than I do, I'm scared that using place will lay traps that I will fall into later.
    Geometry managers have nothing to do with perl (knowledge). Use what fits your brain/needs, and don't worry about it.

    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: Tk: pack, grid or place?
by rinceWind (Monsignor) on May 13, 2005 at 15:29 UTC
    Are there strong reasons for using certain geometry managers?

    Yes there are, as far as I am concerned. Here are my guidelines.

    1. pack
    2. This is the simplest of the geomentry managers to use. I think of it as a concertina cardboard box with books stacked against each of the sides. Imagine the concertina contracts to minimise the space in the middle.

      Most of the time, you just stack against one side. However, the other options are useful if you want to add widgets in a different order.

    3. grid
    4. Pack is all very well for simple apps. And you can nest frames with widgets packing in different directions.

      But, the rows and columns will not line up. If the model of what you want to do fits better a matrix of cells, the grid geometry manager gives you this. Think Excel spreadsheet, or better, HTML table.

    5. place
    6. With the other two, positioning of widgets is automatic, and the size of the tol level window fits the size of what it contains.

      The place geometry manager, on the other hand, works like more traditional GUI programming languages, e.g. Visual Basic. Each widget is placed with coordinates of its origin and an exact size.

      place is used when the user has the ability to drag and move widgets around. However, in Tk, this type of activity tends to be done with a canvas.

    There are other geometry managers, but these tend to be more specialised and less widely used.

    I'm Not Just Another Perl Hacker

Re: Tk: pack, grid or place?
by spurperl (Priest) on May 13, 2005 at 10:23 UTC
    Since you are a newbie, I strongly suggest starting with pack, for two reasons:

    1. Precisely what you mentioned - in the docs and tutorials, pack is used. In the beginning you will work a lot with tutorials and docs, so it's better to have a common language.
    2. Pack is the simplest manager in Tk. Use it until you find it limiting (I *still* haven't found it limiting, except for singular cases where grid is an obvious solution). Once that happens, move to more complex managers.
Re: Tk: pack, grid or place?
by merrymonk (Hermit) on May 13, 2005 at 19:09 UTC
    Personally I have found that grid works extremely well.
    I accept that it can sometimes be a problem getting a reasonable layout
    and that the row and column span
    features can seem to work illogically
    although I am sure this is in my mind more than Tk!.
    However I find that you can easily cut and paste 'sections'
    from one application to another.
Re: Tk: pack, grid or place?
by milarepa (Beadle) on Sep 11, 2008 at 15:40 UTC
    I started using pack because in most documentation said it's the simplest geometry manager. Honestly at the beginning I felt like a daunting task, but with practice I managed to produce the results I wanted.
    Besides taking care of which side you "stack" your widgets, I found that the most important thing with pack is the order in which you place your widgets. For example, I wanted to put several checkbuttons one after the other horizontally. Then I wanted a button just below these checkbuttons. However, after minutes of trial and error I found that the code for the button should go first than the checkbuttons. Here is a snapshot of the code:

    #!/usr/perl/bin/perl use Tk; use warnings; use strict; my ($search, $mw, $entry, $text, $io, $checks); my $id = 0; $mw = MainWindow -> new; $mw -> title("AIM"); $mw -> bind('<Key-Escape>' => sub { exit } ); #**************** text ************************* my $scrollbar = $mw->Scrollbar( ); $scrollbar->pack(-side => 'right', -fill => 'y'); $text = $mw -> Text(-font=>"{Verdana} 10 {bold}", -width => 50, -heigh +t => 25, , -yscrollcommand => ['set' => $scrollbar]) -> pack(-side=>'right'); $scrollbar->configure(-command => ['yview' => $text]); #**************** entry ************************* $entry = $mw -> Entry(-font=>"{Verdana} 10 {bold}", -textvariable => \ +$search) -> pack(-side=>'top'); $entry -> bind('<Key-Return>' => \&search ); $entry -> focus; #**************** button ************************* my $btn_clip = $mw -> Button(-text=>"Copy to Clipboard", -command=>sub {$text->clipboardAppend($text->get( +"1.0", 'end'));}) ->pack(-side=>'bottom', -expand=>'1', -anchor=>'n +w'); #*********** checkbutton ************************* foreach (qw/address telephone keyword all/) { $checks = $mw -> Checkbutton( -text => $_) -> pack(-side=>'lef +t', -anchor=>'n'); } MainLoop;
    Hope this helps...!!!!
      For me, I found it easier to get the layout I want by packing groups of things into separate frames, e.g., for the checkboxes, I'd just pack a frame to the top, then pack each checkbox to the left in the frame.
        Yes, you are right.
        Another way is to group related widgets into frames. That's exactly what I did with LaTeX Wizard 708332. Honestly when I finish it I felt I was depending too much on frames, so on the next project I wanted to rely more on my ability to use the pack geometry manager. Just for practice and fun.... ;-) !!!!
        That's how we learn new things.
        But Thanks anyway for your comments.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://456624]
Approved by blazar
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-04-23 21:47 GMT
Find Nodes?
    Voting Booth?