leonardo-vittorio has asked for the wisdom of the Perl Monks concerning the following question:

here's my code:
use Tk; my $mw = Tk::MainWindow->new; my $fr = $mw->Frame(-label=>"myframe"); my $l1 = $fr->Label(-text=>"label1"); my $l2 = $fr->Label(-text=>"label2"); $fr->grid; $l1->grid; $l2->grid; MainLoop;
Running that ends up in a freeze (and 100% cpu load).
But this is not about competing layout managers, at least not explicitly.
Exchanging all grid calls to pack makes the application run as expected.
Making the labels direct daughters of the main window (as in the examples in those tutorials) also works fine.
It's not about grids arguments: I tried a lot of variations, no chance.
My question: How can I layout a set of widgets within a frame using grid ?
I'd like to use the $widget->grid(-row=>2,-column=>3)-style of using grid.

thanks for any hint !

Replies are listed 'Best First'.
Re: Tk grid in a Frame => freeze
by zentara (Cardinal) on Jul 01, 2009 at 18:12 UTC
    Always go with a Scrolled Pane in Tk. If you want a grid, stack 3 frames into the Scrolled Pane, and split each frame into left/right. Make sure to set expand in the Frame packing options.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      I have a few small Perl/Tk apps that all use the grid method (partly because of specperl) and it seems to work okay. Is there a downside to grids that caused you to avoid using them?

      Elda Taluta; Sarks Sark; Ark Arks

        Use it until you find people complaining about it. :-) Just remember that "pack" is the recommended method. Gtk2 only has a "pack" method of loading windows.

        IIRC, it has todo with designing window widgets, so that they expand correctly on resizing. It probably leads to the never ending discussion.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku
Re: Tk grid in a Frame => freeze
by Anonymous Monk on Jul 01, 2009 at 13:53 UTC
    Here is a working grid example
    #!/usr/bin/perl -- use warnings; use strict; use Tk; my $mw = tkinit; my $topframe = $mw->Frame(-bg=>'yellow')->grid( -row => 2, -column => +2 ); $topframe->Button( -text => "But 0 0")->grid( -row => 0, -column => 0 +); $topframe->Button( -text => "But 1 1")->grid( -row => 1, -column => 1 +); my $other = $topframe->Frame(-bg=>'red')->grid( -row => 2, -column => +0 ); $other->Button( -text => "Oth 0 0")->grid( -row => 0, -column => 0 ); $other->Button( -text => "Oth 1 1")->grid( -row => 1, -column => 1 ); $other->Button( -text => "Oth 2 0")->grid( -row => 2, -column => 0 ); $mw->MainLoop;
      hi Anonymous,
      thanks for Your example.
      In the mean time I found another atomic change to make my original code run without freeze:
      removing the frames label:
      ... my $fr = $mw->Frame; ...

      Using pack instead of grid the labelled frame works as expected. Is there any (documented) relationship between a frames label attribute and the used layout manager ?
        Hi,

        I don't know if it is documented. The Frames 'Label' gets packed itself and you can not use two managers for the same Widget.

        Christoph