Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Perl/Tk Packing and Resizing Question

by golux (Pilgrim)
on Mar 03, 2012 at 05:23 UTC ( #957608=perlquestion: print w/ replies, xml ) Need Help??
golux has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

I'm writing a perl/Tk app, and can't seem to get past one obstacle.

Here is a simpler example program that demonstrates the issue:

#!/usr/bin/perl -w ############### ## Libraries ## ############### use strict; use warnings; use Tk; use Tk::Font; ################## ## Instructions ## ################## my $help = qq{ Instructions: (1) Click "Minimize" followed by "Maximize", and notice how the Canvas alternates between visible and hidden. (2) While the Canvas is maximized, resize the GUI with the mouse, by clicking on and dragging any corner of the GUI to a new place. (3) Finally, click "Minimize" again, and note that the GUI keeps the size of Maximized window, rather than shrinking as before. }; ################## ## Main Program ## ################## my $state = 'max'; # Create the Toplevel my $mw = new MainWindow(-title => "Pack/Repack Example"); # Assign colors, fonts, reliefs my $font1 = $mw->Font(-family => 'arial', -size => 24); my $font2 = $mw->Font(-family => 'arial', -size => 16); my @btn = (-font => $font1, -bg => '#ffef3f'); my @lbl = (-font => $font2, -bg => 'pink', -relief => 'groove'); # Assign widgets my $frm1 = $mw->Frame->pack(-fill => 'x'); my $frm2 = $mw->Frame->pack(-fill => 'x'); my $frm3 = $mw->Frame->pack(-expand => 1, -fill => 'both'); my $btn1 = $frm1->Button(@btn, -text => 'Minimize'); my $btn2 = $frm1->Button(@btn, -text => 'Maximize'); my $btn3 = $frm1->Button(@btn, -text => 'Exit (^X)'); my $can = $frm3->Canvas(-bg => 'white'); my $lbl = $frm2->Label(-text => $help, @lbl)->pack(-fill => 'x'); # Pack the buttons and canvas $btn1->pack($btn2, $btn3, -side => 'left'); $can->pack(-expand => 1, -fill => 'both'); # Draw some random circles in the Canvas for fun for (my $i = 0; $i < 512; $i++) { my $radius = 8 + int rand(64); my ($x0, $y0) = (int rand(800), int rand(800)); my ($x1, $y1) = ($x0 + $radius, $y0 + $radius); my $fill = sprintf "#%02x%02x%02x", rand 256, rand 256, rand 256; $can->createOval($x0, $y0, $x1, $y1, -fill => $fill); } # Attach callbacks and bindings $btn1->configure(-command => sub { minimize() }); $btn2->configure(-command => sub { maximize() }); $btn3->configure(-command => sub { exit }); $mw->bind("<Control-x>" => sub { $btn3->invoke }); MainLoop; ################# ## Subroutines ## ################# sub minimize { ($state eq 'min') and return; $state = 'min'; $frm3->packForget; } sub maximize { ($state eq 'max') and return; $state = 'max'; $frm3->packConfigure(-expand => 1, -fill => 'both'); }

You can click the "Minimize" button and the frame holding the Canvas will become hidden (ie. packForget), followed by the "Maximize" button to restore it.

The problem arises if, while the GUI is maximized, you click with the mouse on any corner and resize the entire GUI (larger -or- smaller), and then click on the "Minimize" button again. What occurs is that the GUI then retains the size of the maximized window rather than shrinking as before.

Of course, I could probably delete $frm3 (the frame holding the Canvas), and recreate it along with the Canvas object, or maybe even just reparent the Canvas object into a new Frame instance. But please bear in mind that my actual application (>1600 lines) has numerous levels of widgets which would be very tricky and memory/time-intensive to recreate after each resize.

Thanks in advance for any insights!

Update:  Many thanks, Anonymous Monk; your link to the similar problem was very helpful, and the solution exactly what I needed!

I went back to my original program, and verified the solution there as well.

Here is my updated minimize() subroutine, now working as expected:

sub minimize { ($state eq 'min') and return; $state = 'min'; $frm3->packForget; # Restore "minimal" dimensions, even if user resized GUI # References: # http://perlmonks.com/?node_id=942166 # http://perlmonks.com/?node_id=957608 $mw->geometry(undef); $mw->update; }
say substr+lc crypt(qw $i3 SI$),4,5

Comment on Perl/Tk Packing and Resizing Question
Select or Download Code
Re: Perl/Tk Packing and Resizing Question
by Anonymous Monk on Mar 03, 2012 at 07:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://957608]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-07-25 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (171 votes), past polls