Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Perl/Tk code structure

by elef (Friar)
on Jan 11, 2012 at 09:47 UTC ( #947318=note: print w/ replies, xml ) Need Help??


in reply to Re: Perl/Tk code structure
in thread Perl/Tk code structure

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?


Comment on Re^2: Perl/Tk code structure
Download Code
Re^3: Perl/Tk code structure
by zentara (Archbishop) on Jan 11, 2012 at 10:42 UTC
    "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.
        Here is another example, using the canvas widget.
        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; my $vh = $mw->vrootheight; my $vw = $mw->vrootwidth; $mw->geometry($vw.'x'.$vh.'+0+0'); $mw->overrideredirect(1); #grabs full control $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size=>int(-18*18/14)); my $topframe = $mw->Frame->pack(); my $frame = $mw->Frame->pack(); my $count = 0; # instead of a button you could make navigation arrows $topframe->Button(-text => "Change Screen", -command => sub{ #clean out frame's children my @w = $frame->packSlaves; foreach (@w) { $_->packForget; } #clean out frame $frame->packForget; $count++; &build($count); })->pack(-side=>'left',-padx=>20); $topframe->Button(-text => "Exit", -command => sub {exit})->pack(-side=>'right',-padx=>20); #make these widgets globals so you reuse them #and avoid memory gains of making too many #redundant widgets my $txt = $frame->Text(-bg=>'white',-font=>'big', -height=>5)->pack(-fill=>'x'); # Note that the 'virtual window' height and width are $vh and $vw # respectively, so we use those dimensions for our Canvas height # and width, and let the Canvas expand and fill in both x and y # directions. # my $canvas = $frame->Canvas( -width => $vw, -height => $vh, -background =>'blue', )->pack(-expand => 1, -fill => 'both'); &build(0); #setup first screen MainLoop; sub build { my $count = shift; #clean out old txt and reuse widget, the pack it again $txt->delete('1.0','end'); $txt->insert('end',"\t\t\t\t Page $count"); $txt->pack(-fill=>'x'); #clean out old canvas and reuse widget, the pack it again $canvas->delete('all'); $canvas->createRectangle(80, 80, 200, 200, -fill => 'yellow'); $canvas->createText(125, 125, -fill => 'black', -text => "page $count", -font => 'big', ); $canvas->pack(-expand => 1, -fill => 'both'); $frame->pack(); #reshow it by repacking the new frame $mw->update; }

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://947318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-10-01 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (40 votes), past polls