kranthi has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to perl/Tk. I am trying to create a frame which adds widgets(Label and Entry widgets) dynamically using grid manager. But unable to delete the widgets within a given range. can you pls help me out in how to delete the widgets of a given range. Regards, Kranthi
  • Comment on generating and destroying widgets dynamically

Replies are listed 'Best First'.
Re: generating and destroying widgets dynamically
by zentara (Cardinal) on May 19, 2008 at 13:24 UTC
    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