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

I'm working on a personal project using Tk, with which I am not too familar. The basic idea that I'm trying to accomplish now is to get a window that contains two frames, only one of which is visible at any one time and both of which occupy the same area in the window. In the example code below, there are two buttons. One button should display the first frame ($frame1), hiding $frame2, and the other button should display $frame2, hiding $frame1.

I've got it partially working, in that clicking either button shows the proper frame and hides the other, but I cannot seem to get the two frames to occupy the same space, nor can I make it so that either (or both) of the frames are hidden when the window is first displayed (before a button is clicked).

I'm using pack() as my geometry manager, and would prefer to continue doing so if possible/convenient. To hide/show the frames, I'm using MapWindow() and UnmapWindow() as those were the only functions (other than withdraw(), which doesn't seem to work for frames) to do such things. Ideally, I would like to find an "unpack()" method that would do the reverse of pack() so that I could just pack() the new frame in.

So, in summary, the two problems I'm having are:

Thanks in advance for any help my fellow Monks are able to give.

(Code below...)

#!/usr/bin/perl -w use Tk; $main = MainWindow->new(); # Container frame for dynamic subframes $fmain = $main->Frame(-borderwidth=>0)->pack(-side=>'top',-fill=>'both +'); # Buttons and button containing frame $bframe = $main->Frame(-borderwidth=>0)->pack(-side=>'bottom'); $bframe->Button(-text=>'Frame 1',-command=>\&showframe1)->pack(-side=> +'left'); $bframe->Button(-text=>'Frame 2',-command=>\&showframe2)->pack(-side=> +'right'); # Dymanic subframes $frame1 = $fmain->Frame(-borderwidth => 0)->pack; $frame1->Label(-text => 'Frame One')->pack; #$frame1->UnmapWindow(); # Doesn't work here $frame2 = $fmain->Frame(-borderwidth => 0)->pack; $frame2->Label(-text => 'Frame Two')->pack; $frame2->UnmapWindow(); # Doesn't work here MainLoop; sub showframe1 { $frame2->UnmapWindow(); # DOES work here, though $frame1->MapWindow(); # elements are not "repacked" } sub showframe2 { $frame1->UnmapWindow(); # Same here, works but $frame2->MapWindow(); # doesn't "repack" }

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.

Replies are listed 'Best First'.
Re: Perl/Tk, MapWindow, UnmapWindow, and dynamic frames
by kschwab (Vicar) on Jul 09, 2001 at 02:12 UTC
    I think you are looking for $widget->packForget();

    If you don't mind tabs instead of buttons, you could try Tk::TabFrame or Tk::Notebook to do the work for you.

      Ah, perfect! Exactly what I was looking for. Now, why didn't I see packForget() when I was looking in the docs? I see it now, of course. :-p

      Update: Oh, and about the notebook/tabbed frame suggestion... I was actually going to have the frames be switched by a radio button so the tabs aren't really what I was looking for, but thanks!

      Thanks!

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.