http://www.perlmonks.org?node_id=1218559

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

I have been struggling with the -sticky option with Tk::grid. It does not seem to want to stretch a container for me.

In this simple example I use $gridOrPack to select whether I will grid or pack 2 frames. Then I add a label to one frame and a button to the other frame...

when I set $gridOrPack to 'pack' I pack with -expand=>1 and fill=>'both', It behaves just like I want... the frames stretch to fill the mainWindow

when I set $gridOrPack to 'grid' I grid with the -sticky option set to 'nsew' (I've also tried 'ew' and 'ns') and the frames do not stretch.

Am I missing something silly?

use strict; use Tk; use Tk::LabFrame; use Tk::Treeplus; use Tk::ErrorDialog; my $mw = MainWindow->new(); my $gridOrPack="grid"; #"pack"; #my $gridOrPack="pack"; my $tpFrame; my $butFrame; my $tpFrame=$mw->LabFrame(-label=>'TreePlusFrame'); my $butFrame=$mw->LabFrame(-label=>'ButtonFrame'); if ($gridOrPack=~/grid/i) { $tpFrame->grid(-row=>1, -column=>1, -sticky=>'ew'); $butFrame->grid(-row=>2, -column=>1, -sticky=>'ew'); } else { $tpFrame->pack(-side=>'top',-expand=>1,-fill=>'both'); $butFrame->pack(-side=>'top',-expand=>1, -fill=>'both'); } $tpFrame->Label(-text=>'I like pack better')->pack(-side=>'top'); $butFrame->Button( -text => 'Exit', -command => sub { exit(0) }, ) ->pack(qw(-side bottom -pady 10)); MainLoop();

Replies are listed 'Best First'.
Re: Tk::grid -sticky option not stretching frames
by tybalt89 (Monsignor) on Jul 16, 2018 at 13:44 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1218559 use strict; use warnings; use Tk; use Tk::LabFrame; #use Tk::Treeplus; use Tk::ErrorDialog; my $mw = MainWindow->new(); my $gridOrPack="grid"; #"pack"; #my $gridOrPack="pack"; #my $tpFrame; #my $butFrame; my $tpFrame=$mw->LabFrame(-label=>'TreePlusFrame'); my $butFrame=$mw->LabFrame(-label=>'ButtonFrame'); if ($gridOrPack=~/grid/i) { $tpFrame->grid(-row=>1, -column=>1, -sticky=>'nsew'); $butFrame->grid(-row=>2, -column=>1, -sticky=>'nsew'); $mw->gridRowconfigure( 1, -weight => 1 ); $mw->gridRowconfigure( 2, -weight => 1 ); $mw->gridColumnconfigure( 1, -weight => 1 ); } else { $tpFrame->pack(-side=>'top',-expand=>1,-fill=>'both'); $butFrame->pack(-side=>'top',-expand=>1, -fill=>'both'); } $tpFrame->Label(-text=>'I like pack better')->pack(-side=>'top'); $butFrame->Button( -text => 'Exit', -command => sub { exit(0) }, ) ->pack(qw(-side bottom -pady 10)); MainLoop();

      Thankyou gracious tybalt89! I've not used the gridRowconfigure gridColumnconfigure methods before but that sure fixes my problem!

Re: Tk::grid -sticky option not stretching frames
by kcott (Archbishop) on Jul 16, 2018 at 13:00 UTC

    G'day boleary,

    I don't have time to code a test; however, it looks like your problem could be that you're assuming rows and columns start at one. See Tk::grid - they both start at zero.

    — Ken

      Thanks for checking in Ken... changed to -row=>0, -column=>0 no effect... In all my usage of grid, it doesn't seem to care about your starting row/col num

Re: Tk::grid -sticky option not stretching frames
by zentara (Archbishop) on Jul 17, 2018 at 11:36 UTC
    IMHO, using pack is a far better solution than using grid. Why don't you just use pack to begin with? Pack works best.

    I'm not really a human, but I play one on earth. ..... an animated JAPH
        Hi, yes, in your particular example, grid is probably easier. Pack is generally preferred because it usually resizes better, but grid is there to use for a good reason, as your example shows. using grid by Lidie explains the basic problems.

        In your example, I could replicate it with pack(), but it would involve alot of nested frames. When you move up to more advanced toolkits like Gtk2, Wx, etc. everything is done using packed vbox, hbox and table items. So, I generally give the advice to try and think in terms of using packing and tables.

        Also I might add, that Tk::LabFrame is very useful in aligning.


        I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: Tk::grid -sticky option not stretching frames
by Anonymous Monk on Jul 16, 2018 at 12:30 UTC
    Youre using grid when you want pack?

      I want to use grid, but the grid(-sticky=>'nsew') option which is supposed to subsume the combined role of pack(-expand=>1, -fill=>'both') does not seem to work for me

        :) You said: I have a solution, but I want to use something else, Am I missing something silly? Think about it. Use the solution. Duh!