in reply to passing TK object into a sub

I have trouble understanding your example, but here is how to do correct packForgetting. Notice, you reuse your widgets, NOT make new ones. While they are in a packForget state, you can reconfigure them, then repack them.
#!/usr/bin/perl use warnings; use strict; use Tk; my $count = 0; my $top = new MainWindow; my $txt = $top->Scrolled("Text")->pack; $txt->insert('end',"Original stuff\n"); my $button1 = $top->Button(-text => "packForget", -command => sub{ my @w = $top->packSlaves; foreach (@w) { $_->packForget; } &rebuild; })->pack(); my $button = $top->Button(-text => "Exit", -command => sub {exit})->pack; MainLoop; sub rebuild{ $txt->delete('0.0','end'); $txt->insert('end',time."New stuff\n"); $txt->pack; $count++; $button1->configure(-text=>'packForget'.$count); $button1->pack; $button->pack; } __END__

I'm not really a human, but I play one on earth CandyGram for Mongo

Replies are listed 'Best First'.
Re^2: passing TK object into a sub
by vortmax (Acolyte) on Jul 16, 2008 at 15:19 UTC
    Im my example I just have a window with a menubar at the top. Selecting a mode from the "mode" menu displays different content in the main window.

    So I built all of that and put a frame below the menu to house whatever content I need to load into it. Which is loaded by calling a specific sub.

    So by your example, I should build all of the widgets ahead of time and just pack/forgetPack them with the subs to change their visibility? Does that have an adverse effect on memory usage?
      You are correct. Build all of your widgets ahead of time, and control their visibility by packing/packForgetting them. Your memory usage will stay constant that way. If you are talking about many widgets, that may never get used, only build them as you need them, and store them in a hash or array for easy manipulation. So you can do something like this, where you can set the variable for the checkbox, to $num, and when you click the checkbox, the $num will be sent to the sub.
      my %entry; #global to store entries sub get_entry{ my $num = shift; if ( !Exists( $entry{$num}{'entry'} ) { $entry{$num}{'entry'} = $frame->Entry()->pack(); } else { #repack $entry{$num}{'entry'}->pack(); } }

      I'm not really a human, but I play one on earth CandyGram for Mongo
        awesome, thank you very much