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

Is there a protocol to catch when the user maximize the main window? I use the following to catch when the user closes the application, but I can't find any counterpart to catch the maximization (or minimization) of the main window:

$mw->protocol('WM_DELETE_WINDOW', \&CloseApp);

Thank you in advance for any suggestion.

Replies are listed 'Best First'.
Re: Tk main window protocole maximize
by tybalt89 (Monsignor) on Apr 24, 2018 at 19:52 UTC

    You can get the new size when it is changed by binding to Configure.

    Here's an example that does that:

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1213422 use strict; use warnings; use Tk; my $mw = MainWindow->new; my $c = $mw->Canvas( -width => 400, -height => 400, -bg => 'red', -highlightthickness => 0, )->pack; for my $row (0..7) { my $color = $row % 2; for my $col (0..7) { $c->createRectangle($row*50, $col*50, $row*50+50, $col*50+50, -fill => qw( gold2 green)[$color++ % 2], -outline => undef, ); } } $mw->bind('<Configure>' => sub { print "width ", $mw->width, " height ", $mw->height, "\n"; } ); MainLoop;
Re: Tk main window protocole maximize
by choroba (Cardinal) on Apr 24, 2018 at 19:16 UTC
    No, maximizing a window is not a protocol thing. In fact, Inter-Client Communication Conventions Manual mentions only 3 protocols: WM_TAKE_FOCUS, WM_SAVE_YOURSELF, and WM_DELETE_WINDOW.

    Rosetta Code mentions the zoomed state as a way of maximizing a window, it doesn't work on other platforms, though. It also doesn't provide a way to capture the event.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Tk main window protocole maximize
by zentara (Cardinal) on Apr 25, 2018 at 12:00 UTC
    As a practical workaround solution, you can use tybalt89's solution, and check the current width and height vs. the maximum screen size in the configure subroutine callback. Another less desirable hack is to use overrideredirect, to remove the Window Manager controls, and then make your own maximize button.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $top = MainWindow->new; $top->geometry('200x200+200+200'); $top->overrideredirect(1); $top->Button( -text => 'Maximize', -command => sub { $top->geometry($top->screenwidth . 'x' . $top->s +creenheight . '+0+0'); print "Maximized\n"; })->pack; $top->Button( -text => 'Exit', -command => sub { $top->destroy } )->pack; MainLoop;

    I'm not really a human, but I play one on earth. ..... an animated JAPH