Don't try to use a create/destroy cycle to control widgets in Tk. Almost certainly, you will accumulate memory usage with each cycle. Tk is not good about cleaning up after itself( Gtk2 is better, but also is not perfect).

The general rule is to use gridForget, reconfigure the widgets in the frame, then regrid everything. This way you reuse a few widgets.

Here is an example using pack, but you should be able to convert to grid.

#!/usr/bin/perl use warnings; use strict; my @stn_init = ( "station1", "station2", "station3" ); my @stn_sup = ("station1", "station2", "station3", "stationa", "statio +nb" ); my @stn = @stn_init; my @station_frames; my @z; my @w; my @b; my @stations; ###################################################################### +########### $ENV{ TZ } = "PST08PDT07"; use Tk; my $mw = MainWindow->new; $mw->minsize( qw(250 150) ); $mw->title( "Metar Watch" ); $mw->configure( -background => 'tan' ); my $menu_bar = $mw->Frame( -relief => 'groove', -borderwidth => 3, -background => 'b +lue', ) ->pack( '-side' => 'top', -fill => 'x' ); my $file_mb = $menu_bar->Menubutton( -text => 'File', -background => 'blue', -activebackground => 'salmon', -foreground => 'white', )->pack( -side => 'left' ); $file_mb->command( -label => 'About', -background => 'blue', -activebackground => 'salmon', -command => \&ABOUT ); $file_mb->command( -label => 'Exit', -background => 'blue', -activebackground => 'salmon', -command => sub { $mw->destroy } ); my $top = $mw->Scrolled('Pane', -scrollbars=>'osoe', )->pack( -side => 'top', -fill => 'x' ); # make header my $header_f= $top->Frame->pack(); my $station0 = $header_f->Label( -text => 'Station ', )->pack(-side => + 'left', -pady => 9, -padx => 8 ); my $z0 = $header_f->Label( -text => 'Time (Z)', )->pack(-side => 'left +', -pady => 9, -padx => 8); my $w0 = $header_f->Label( -text => 'Wind (KT)', )->pack(-side => 'lef +t', -pady => 9, -padx => 8); #make control frame my $ob_bar = $mw->Frame( -relief => 'groove', -borderwidth => 3, -background => 'o +range', ) ->pack( '-side' => 'bottom', -fill => 'x' ); my $other_obs=0; my $ob_lab = $ob_bar->Checkbutton( -text => 'Show more sites', -variable => \$other_obs, -foreground => 'black', -background => 'orange', -command => \&rebuild )->pack( -side => 'left' ); my $other_buoys=0; my $buoy_check = $ob_bar->Checkbutton( -text => 'Show more buoys', -variable => \$other_buoys, -foreground => 'black', -background => 'orange', -command => \&rebuild )->pack( -side => 'left' ); &build; MainLoop; sub rebuild{ if($other_obs==1){ @stn = @stn_sup }else{ @stn = @stn_init } foreach my $fr (@station_frames){ my @w = $fr->packSlaves; foreach (@w) {$_->packForget;} $fr->packForget; $mw->update; } &build; } sub build { #make frame for each station my $n = 0; for ( 0 .. $#stn ) { $station_frames[$_]= $top->Frame->pack(-expand=>1, -fill=>'both'); $stations[ $n++ ] = $station_frames[$_]->Label( -text => "$stn[$n] +" ) ->pack(-side =>'left', -pady => 1 ); $z[ $n ] = $station_frames[$_]->Entry( -justify => 'center', -background => 'light yellow', -width => 5, -borderwidth => 2, -relief => 'sunken' )->pack(-side => 'left', -pady => 2, -padx => 10); $w[ $n ] = $station_frames[$_]->Entry( -justify => 'center', -background => 'light green', -width => 7, -borderwidth => 2, -relief => 'sunken' )->pack(-side => 'left', -pady => 2, -padx => 10); if($other_buoys==1){ $b[ $n ] = $station_frames[$_]->Entry( -justify => 'center', -background => 'light green', -width => 7, -borderwidth => 2, -relief => 'sunken' )->pack(-side => 'left', -pady => 2, -padx => 10); } } $mw->update; }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re: generating and destroying widgets dynamically by zentara
in thread generating and destroying widgets dynamically by kranthi

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.