http://qs1969.pair.com?node_id=942166

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

I have a PerlTk app containing a.o a frame that has 2 Scrolled text boxes in it. To minimize the app, there is a button that can show/hide these boxes. Hitting the button calls a PackForget() on the frame and its children, or a pack() on the same, depending on the state of the frame.

Now this works fine as long as the GUI is not being resized manually. After a resize, the text boxes are forgotten and restored, but the main window does not resize to go with it.

I have trimmed down the code as much as possible (see below), and tried many code changes but can't get around the problem. As such there is some 'debug' code still around.

any help is appreciated, David

my code:
use strict; use Tk; use Tk::Menu; use Tk::Text; use Tk::Dialog; use Tk::Scrollbar; use Tk::DialogBox; ##################### my $stdout_shown = 1; ############ script starts here ################## my $mw = MainWindow->new(); $mw->title($0); my $buttons_frame = $mw->Frame(-relief => 'groove')->pack(-side => ' +top', -fill => 'both'); my $top_frame = $buttons_frame->Frame(-relief => 'groove')->pack +(-side => 'top', -fill => 'x'); my $middle_frame = $buttons_frame->Frame(-relief => 'groove')->pack +(-side => 'top', -fill => 'x'); &load_task_list(); # loads the tasks from the task file and creates a +button for each. &create_ctrl_buttons(); # see how much space is needed without the log_frame $mw->idletasks; my $w = $mw->reqwidth; my $h = $mw->reqheight; print "MINSIZE: $w $h\n"; $mw->minsize($w,$h); $mw->update(); my $log_frame = $mw->Frame(); #$log_frame->packPropagate(1); my $scr_stdout = $log_frame->Scrolled("Text", -scrollbars => 'oso +e',-relief => 'groove', -height => 10)->pack(-fill => 'both', -expand + => 1); my $scr_stderr = $log_frame->Scrolled("Text", -scrollbars => 'oso +e',-relief => 'groove', -height => 5)->pack(-fill => 'both', -expand +=> 1); # -width => 150, $log_frame->pack(-side => 'bottom', -fill => 'both', -expand => 1); $log_frame->packPropagate(1); print STDERR "this is STDERR before tie\n"; print STDOUT "this is STDOUT before tie\n"; MainLoop; exit 0; ################# my @logframe_kids = (); my $stdout_geo; my $stdout_w; my $stdout_h; sub mw_show_hide($) { my $lbtn = $Tk::widget; #$mw->idletasks; my $lframe_w = $log_frame->width; my $lframe_h = $log_frame->height; $stdout_w = $scr_stdout->width; $stdout_h = $scr_stdout->height; $stdout_geo = $scr_stdout->geometry(); # nee +d to adjust for task-bar xx pts my $lframe_rw = $log_frame->reqwidth; my $lframe_rh = $log_frame->reqheight; my $stdout_rw = $scr_stdout->reqwidth; my $stdout_rh = $scr_stdout->reqheight; my $stderr_rw = $scr_stderr->reqwidth; my $stderr_rh = $scr_stderr->reqheight; # warning_window("sizes: frame: c($lframe_w x $lframe_h) r:($lframe_rw + x $lframe_rh) \n stdout: g:($stdout_geo) c($stdout_w x $stdout_h) r( +$stdout_rw x $stdout_rh) \n stderr: $stderr_rw x $stderr_rh"); # print "log-frame " . ($log_frame->ismapped ? 'is' : 'is not') . " ma +pped!\n"; if ($stdout_shown) # it was visible { $stdout_shown = 0 ; # invert the boolean state; hide the window + and add the 'show' button + message $lbtn->configure(-text => 'Show'); @logframe_kids = $log_frame->packSlaves; print "frame slaves:",@logframe_kids,"\n"; foreach (@logframe_kids) { $_->packForget; } $log_frame->packForget; } else { $stdout_shown = 1 ; # invert the boolean state; show the window +and add the 'hide' button + message $lbtn->configure(-text => 'Hide'); print "frame slaves:",@logframe_kids,"\n"; if (scalar(@logframe_kids) == 0) {warning_window("nothing to re-pa +ck here !");} $scr_stdout->configure(-width => $stdout_w , -height => $stdout_h) +; foreach (@logframe_kids) { $_->pack; } $log_frame->pack; } $mw->update; } sub create_ctrl_buttons { my $show_hide_txt = $stdout_shown ? 'Hide' : 'Show'; my $stop_btn = $top_frame->Button(-text => 'Stop', -comma +nd => [\&warning_window, "STOP" ] )->pack(qw/-side left -anchor w/); my $paus_btn = $top_frame->Button(-text => 'Pause', -comma +nd => [\&warning_window, "PAUSE"] )->pack(qw/-side left -anchor w/); my $show_hide_btn = $top_frame->Button(-text => 'Show/hide', -comma +nd => \&mw_show_hide ) ->pack(qw/-side right -anchor e/); } sub load_task_list { my @tsks = qw/t1 t2 t3 t4/; foreach my $t (@tsks) { my $btn = $middle_frame->Button(-width => 4, -text => $t, -comman +d => [\&warning_window ,"$t"] )->pack(qw/-side left -anchor w -fill x + -expand 1/); } } sub warning_window { # print a message and wait for OK to be clicked. my(@args) = @_; my $warn_txt = "WARNING: @args"; my $ww = $mw->Dialog(-title => 'Warning', -text => $warn_txt, -buttons => ['Ok'], -default_button => 'Ok'); my $answer = $ww->Show(); return ($answer); }

Replies are listed 'Best First'.
Re: resizing problem with Tk appl using PackForget
by Anonymous Monk on Dec 07, 2011 at 07:39 UTC

    one tip, when describing the problem widgets, use the variable names from the code you post

    When you pack, you forget to pack( qw/ -fill both  -expand 1 / ), and without those arguments, the widgets won't grow with resizing

    If you make it into a widget, you can reuse this thing :)

      Hi Anonymous,

      // thx for the tip

      Your hint indeed fixed to resize the hidden widgets to their original state. Do you perhaps know of an easy way to store/remember the pack arguments for each widget? In my real app the pack arguments are more diverse. What do you mean by "if you make it into a widget" ??

      This does leave the problem that after a resize, the 'hide' action does not minimize the container frame. Any idea how to get that fixed?

      thx David

        Do you perhaps know of an easy way to store/remember the pack arguments for each widget?

        Make it a widget (read object)

        my $rememberer = MakeHideDaddyChildButton( -buttonDaddy => $top_frame, -hidersDaddy => $mw, # gets resized -hiders => [ $top_frame, $middle_frame ], #daddys kiddys ); $rememberer->pack;

        This does leave the problem that after a resize, the 'hide' action does not minimize the container frame. Any idea how to get that fixed?

        Um, repack the container frame (or its parent, or mainwindow ) .... or better idea, only ever forget/repack the container frame

        Might need to resize (->geometry)

        I'd have posted an example, but I was already working on this one, keep shooting, and watch the canvas resize :) the mainwindow doesn't resize

        #!/usr/bin/perl -- use strict; use warnings; use Tk; my $packArgs = do { my @packargs = ( [ qw/-expand 0 / ], [ qw/-expand 1 -fill x / ], [ qw/-expand 1 -fill y / ], [ qw/-expand 1 -fill both / ], ); my $ix = -1; my $xx = @packargs ; sub { $ix++; warn $ix % $xx ; return @{ $packargs[ $ix % $xx ] }; }; }; my $mw = tkinit; $mw->Button( -text => 'BANG!', -command => \&bangBang )->pack(qw/-side top -anchor n/); my $ca = $mw->Canvas( background => 'white' ) ->pack( $packArgs->() ); $ca->createOval( 50, 50, 100, 100, -fill => 'pink', ); MainLoop; exit 0; sub bangBang { my $b = $Tk::event->W; $mw->Busy; $b->configure(-state => "disabled"); my $text = $b->cget(qw/ -text /); unless( $text =~ s/(\d+)/ $1 + 1 /e ){ $text .= " 1"; } $b->configure( -text => $text); $ca->packForget; $mw->update; sleep 1; $ca->pack( $packArgs->() ); $mw->Unbusy; $b->configure(-state => "normal"); $mw->update; }