Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

In reply to Perl/Tk Packing and Resizing Question by golux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (7)
    As of 2014-12-28 18:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (182 votes), past polls