http://qs321.pair.com?node_id=1128001

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

Hi Monks, back again with another Tk question. I want to reuse a frame in main window to show different information based on what the user selects. I am trying to avoid having to destroy and recreate all the widgets as a user goes into a sub section and back out. This is a very simplified working example code of what I want to do (except it does not load the widgets and will eventually run out of mem, hence my Q). Basically there are multiple systems monitored and buttons for each system to show sub information on that system. Thanks.

Only Unmap/Map a FRAME not the widgets within

use Tk; my $mainwindow; my @save; mw(); MainLoop(); sub mw { $mainwindow = MainWindow->new(); my $frm1 = $mainwindow->Frame(-borderwidth=>4, -relief=>"ridge")-> +pack(-side=>'top', -expand=>1, -fill=>"x", -anchor=>"n"); my $frm2 = $mainwindow->Frame(-borderwidth=>4, -relief=>"ridge")-> +pack(-side=>'top', -expand=>1, -fill=>"both", -anchor=>"n"); my $frm3 = $mainwindow->Frame(-borderwidth=>4, -relief=>"ridge")-> +pack(-side=>'top', -expand=>1, -fill=>"x", -anchor=>"n"); $frm1->Label(-text=>"TITLE")->pack(-side=>'top'); $frm3->Button(-text=>"Hide Restore", -command=>sub{ if (@save) { print join("\n",@save)."\n"; foreach my $child (@save) { push(@save, $child); $child->MapWindow(); } } else { print join("\n",$frm2->children())."\n"; foreach my $child ($frm2->children()) { push(@save, $child); $child->UnmapWindow(); } } $mainwindow->update(); })->pack(-side=>'top'); $frm2->Label(-text=>"buttons to specific data")->pack(-side=>'top' +); $frm2->Label(-text=>"buttons to specific data")->pack(-side=>'top' +); $frm2->Label(-text=>"buttons to specific data")->pack(-side=>'top' +); $frm2->Label(-text=>"buttons to specific data")->pack(-side=>'top' +); $frm2->Label(-text=>"buttons to specific data")->pack(-side=>'top' +); }

solution

use Tk; my $mainwindow; my $save=0; mw(); MainLoop(); sub mw { $mainwindow = MainWindow->new(); my $frm1 = $mainwindow->Frame(-borderwidth=>4, -relief=>"ridge")-> +pack(-side=>'top', -expand=>1, -fill=>"x", -anchor=>"n"); my $frm2 = $mainwindow->Frame(-borderwidth=>4, -relief=>"ridge")-> +pack(-side=>'top', -expand=>1, -fill=>"both", -anchor=>"n"); my $frm3 = $mainwindow->Frame(-borderwidth=>4, -relief=>"ridge")-> +pack(-side=>'top', -expand=>1, -fill=>"x", -anchor=>"n"); my $container = $frm2->Frame(-borderwidth=>4, -relief=>"ridge")->p +ack(-side=>'top', -expand=>1, -fill=>"both", -anchor=>"n"); $frm1->Label(-text=>"TITLE")->pack(-side=>'top'); $frm3->Button(-text=>"Hide Restore", -command=>sub{ if ($save) { $container->MapWindow(); $container->raise; $save=0; } else { $container->UnmapWindow(); $save=1; } return; })->pack(-side=>'top'); $container->Label(-text=>"buttons to specific data")->pack(-side=> +'top'); $container->Label(-text=>"buttons to specific data")->pack(-side=> +'top'); $container->Label(-text=>"buttons to specific data")->pack(-side=> +'top'); $container->Label(-text=>"buttons to specific data")->pack(-side=> +'top'); $container->Label(-text=>"buttons to specific data")->pack(-side=> +'top'); }