http://qs1969.pair.com?node_id=11123961


in reply to Perl Tk Resize Question

I had the same issue today and found this thread. Yes, it is old, but I would like to add another solution in case someone needs it.

You can use a timer to wait for a moment before you do the resize. Cancel the timer if the MainWindow resizes again within a certain time and start it anew.

Sample code:

#!perl use strict; use warnings; use Tk; use v5.16; # Skript zum manuellen resizen der Canvas nach Änderung der Größe des +MainWindow. # Man muss nochmal einen Rechtsklick machen. # Quelle: https://www.perlmonks.org/bare/?node_id=581309, abgerufen 20 +20-11-21 my $mw = Tk::MainWindow->new; my $current_size = $mw->reqwidth . "x" . $mw->reqheight; my $old_current_size = $current_size; my $canvas = $mw->Canvas( -width => 300, -height => 300, -bg => 'black', )->pack(); $canvas->update; # important to call this before binding <configure> e +vent my $after_resize_timer_id; $mw->bind('<Configure>' => sub { say "MW resized"; # reset timer again, if it was set $mw->afterCancel($after_resize_timer_id) if defined $after_resize_ +timer_id; # set timer # if MW doesn't resize again within a second or so, resize Canvas # if it does, reset timer to wait for a second or so $after_resize_timer_id = $mw->after(500, sub{ say "Update canvas"; my $width = $mw->width; my $height = $mw->height; $canvas->configure(-width => $width, -height => $height); $canvas->update; }); }); $mw->MainLoop; exit(0);