• Maximize widget sizes

    From =?UTF-8?Q?Josef_M=C3=B6llers?=@21:1/5 to All on Sat Aug 16 22:59:24 2025
    Hi,

    I have a little Perl/Tk program that runs on a 480x320 LCD display on
    top of a RasPi.

    I create a new MainWindow:
    my $mw = MainWindow->new;

    and then populate it with widgets which I arrange in a grid:

    my ($row, $col) = (0, 0);
    foreach (@items) {
    my $name = shift @$_;
    my $label = shift @$_;
    my $func = shift @$_;

    $button{$name} = $mw->Button(-text => $label, -command => [ $func,
    @$_ ])->grid(-row => $row, -column => $col, -sticky => 'news');
    $col++;
    if ($col == $MAXCOLUMNS) {
    $row++;
    $col = 0;
    }
    }
    if ($col != 0) {
    $row++;
    }
    my $status = $mw->Entry(-relief => 'sunken')->grid(-row => $row, -column
    0, -columnspan => 2, -sticky => 'news');
    $row++;
    $mw->Checkbutton(-text => "Erase after copying", -variable => \$erase_after_copying)->grid(-row => $row, -column => 0, -columnspan =>
    2, -sticky => 'news');
    $row++;
    $mw->Button(-text => 'Shutdown', -command => sub { system("sudo
    poweroff") })->grid(-row => $row, -column => 0, -columnspan => 2,
    -sticky => 'news');

    My problem is that the resulting window and the buttons inside are
    fairly small. I tried making the MainWindow larger:
    $mw->geometry('400x250');
    but the buttons do not grow with the MainWindow. They still have the
    same size as without the geometry change.

    What do I need to do to have all the widgets grow to fill the large
    MainWindow?

    Thanks in advance,

    Josef

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to josef@invalid.invalid on Sat Aug 16 22:16:11 2025
    =?UTF-8?Q?Josef_M=C3=B6llers?= <josef@invalid.invalid> wrote or quoted:
    What do I need to do to have all the widgets grow to fill the large >MainWindow?

    An example is provided below; see the comments.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Tk;

    sub say_hello { print "Hello!\n"; }
    sub say_goodbye { print "Goodbye!\n"; }

    my @items = (
    [ 'hello_btn', 'Say Hello', \&say_hello ],
    [ 'goodbye_btn', 'Say Goodbye', \&say_goodbye ],
    );

    my $MAXCOLUMNS = 2;

    my $mw = MainWindow->new;
    $mw->title("Resizable Button Grid Example");

    my %button;
    my ($row, $col) = (0, 0);

    foreach my $item (@items) {
    my $name = shift @$item;
    my $label = shift @$item;
    my $func = shift @$item;

    $button{$name} = $mw->Button(
    -text => $label,
    -command => [ $func, @$item ]
    )->grid(
    -row => $row,
    -column => $col,
    -sticky => 'nsew' # allow widget to stretch in all directions
    );

    $mw->gridColumnconfigure($col, -weight => 1); # make this column expand when window grows

    $col++;
    if ($col == $MAXCOLUMNS) {
    $mw->gridRowconfigure($row, -weight => 1); # make this row expand when window grows
    $row++;
    $col = 0;
    }
    }

    if ($col != 0) {
    $mw->gridRowconfigure($row, -weight => 1); # expand last partially filled row
    $row++;
    }

    my $status = $mw->Entry(-relief => 'sunken')->grid(
    -row => $row,
    -column => 0,
    -columnspan => $MAXCOLUMNS,
    -sticky => 'nsew' # entry expands across both directions
    );
    $mw->gridRowconfigure($row, -weight => 1); # let status row expand
    $row++;

    my $erase_after_copying = 0;
    $mw->Checkbutton(
    -text => "Erase after copying",
    -variable => \$erase_after_copying
    grid(
    -row => $row,
    -column => 0,
    -columnspan => $MAXCOLUMNS,
    -sticky => 'nsew' # checkbox expands with resizing
    );
    $mw->gridRowconfigure($row, -weight => 1); # let this row expand
    $row++;

    $mw->Button(
    -text => 'Shutdown',
    -command => sub { system("sudo poweroff") }
    grid(
    -row => $row,
    -column => 0,
    -columnspan => $MAXCOLUMNS,
    -sticky => 'nsew' # shutdown button stretches
    );
    $mw->gridRowconfigure($row, -weight => 1); # let shutdown row expand

    MainLoop;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Josef_M=C3=B6llers?=@21:1/5 to Stefan Ram on Sun Aug 17 10:16:50 2025
    On 17.08.25 00:16, Stefan Ram wrote:
    =?UTF-8?Q?Josef_M=C3=B6llers?= <josef@invalid.invalid> wrote or quoted:
    What do I need to do to have all the widgets grow to fill the large
    MainWindow?

    [...]
    $mw->gridColumnconfigure($col, -weight => 1); # make this column expand when window grows
    [...]
    $mw->gridRowconfigure($row, -weight => 1); # expand last partially filled row
    [...]

    Ah ... Stefan ... You made my day!
    If you ever come to Paderborn, drop me a hint and I'll invite you over
    for a drink of any kind!

    Thanks and have a nice Sunday,

    Josef

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)