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.


In reply to Perl/Tk, MapWindow, UnmapWindow, and dynamic frames by bbfu

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.