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
-
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.