in reply to Perl/Tk code structure

I'm not sure how to make a Tk GUI that guides the user through several steps and redraws the window on user input (remove the buttons/text and show the buttons for the next step etc.)

What you are looking for is packForget and it's associate methods. You can pack a window with widgets, packforget all or some of the widgets so they are removed from the screen, reconfigure the widgets, then repack them. A simple example:

#!/usr/bin/perl use warnings; use strict; use Tk; my $top = new MainWindow; my @counts = ('a'..'z'); my %cbuttons; my $frame = $top->Frame()->pack(); setup_page(); $top->Button(-text => "packForget", -command => sub{ my @w = $frame->packSlaves; foreach (@w) { $_->packForget; } })->pack(); $top->Button(-text => "repack", -command => sub{ &setup_page })->pack(); $top->Button(-text => "Exit", -command => sub {exit})->pack; MainLoop; sub setup_page{ for (1..4){ my $text = shift @counts; $cbuttons{$_}{'cb'} = $frame->Checkbutton( -text => $text, -variable => \$cbuttons{$_}{'val'}, -command => \&SetState, )->pack; } }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Perl/Tk code structure
by elef (Friar) on Jan 11, 2012 at 09:47 UTC
    Thanks.
    O'Reilly writes that "packForget makes it look like the widget disappears. The widget is not destroyed, but it is no longer managed by pack. The widget is removed from the packing order, so if it were repacked later, it would appear at the end of the packing order."
    Based on that, I assume that packForget comes into play when one wants to "hide" a widget that may be needed later on. When you want to get rid of a widget, frame or window once and for all on a buttonpress, you'd add $thingie-> 'destroy' to the command sub of the button... Right?
      "packForget makes it look like the widget disappears. The widget is not destroyed, but it is no longer managed by pack. The widget is removed from the packing order, so if it were repacked later, it would appear at the end of the packing order."

      Yes, but you are missing the point as to how to use packForget. The widgets are NOT destroyed, that is true, but what you want to do is reconfigure your widgets in the withdrawn state, then reuse the same widget. Destroying and creating/destroying alot of widgets MAY give your program what looks like a memory leak, as widgets with positive ref counts, don't get destroyed. Its an often discussed problem. So reuse your widgets. The widgets are unmapped with packForget, then you use configure on them to reconfigure them with new data, then repack them. Widgets can be reused/recycled.


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        I see. As there won't be more than five or six "screens" in this program with a couple of buttons/entry fields in each, I should be safe either way. I can't imagine that stuff taking up more than a couple MB of memory.