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);
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|