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

Hi, I'm working on some program (Perl/tk), which have a window with canvas, where some numerical data is graphically represented. There should be a possibility to resize the window, together with canvas inside... Ok, the window and canvas do rescale (simply with mouse at the window corner). The problem is, the content of canvas stays as it was. Rescaling of contents is pretty simple, however to do that, I need to know the new width and height of canvas... How to do that? I tried to use
my $newx = $Mycanvas -> cget(-width);
But it does not seem to work (it always shows the initial value). Second problem, how to run rescaling automatically after window has been resized? Is there any binding, or whatever? Of course, I can always make button "Update", but this is not so nice...

Replies are listed 'Best First'.
Re: Automatic canvas content rescaling
by vkon (Curate) on Apr 27, 2006 at 11:56 UTC
    You was very close there:
    perl -MTk -we "$m=tkinit;$c=$m->Canvas->pack(-fill=>'both',-expand=>1) +;$m->Button(-command=>sub{print $c->width})->pack;MainLoop;"
    But its not easy to recalculate all item's X and Y positions.
Re: Automatic canvas content rescaling
by zentara (Cardinal) on Apr 27, 2006 at 15:50 UTC
    Are you looking for the Configure binding? This one is tied to $mw, but you could bind it to the canvas, in case you have an adjuster, or something where the $mw stays the same.
    #!/usr/bin/perl use Tk; my $mw = new MainWindow; my $canvas = $mw->Canvas(-bg=>'white')->pack(-expand=>1, -fill => 'bot +h'); my $currentSize = $mw->reqwidth . "x" . $mw->reqheight; $mw->bind( '<Configure>', [ \&OnResize, \$currentSize ] ); MainLoop; sub OnResize { my ( $mw, $oldSize ) = @_; my $newSize = $mw->width . "x" . $mw->height; if ( $$oldSize ne $newSize ) { ## Resize has occurred do something: printf( "Resize happened - old size: %s, new size: %s\n", $$oldSize, $newSize ); ## set the old size to the new size $$oldSize = $newSize; } }

    I'm not really a human, but I play one on earth. flash japh