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

I'm using the following sub-routine to create butons on the fly for my Tk app, but my problem is that for every call to the routine the new buttons are getting appended to the old. (i.e. my list of buttons grows.) Does anyone know of a simple solution to the problem?
sub button_setup($prog_select) { my $prog = shift; foreach my $key (keys %{$button{$prog}}) { my $key = $buttons->Button( -text => $key, -width => '10', -command => sub {system "$button{$prog}{$key}";} )->pack(-side => 'top', -pady=>'10'); } }

Replies are listed 'Best First'.
Re: Creating dynamic Tk buttons??
by converter (Priest) on Jun 06, 2003 at 17:23 UTC
    $buttons-Button(...)->pack(...) allocates and packs a new Tk::Button widget. If you want to replace existing Button widgets, you can invoke the destroy method against each existing Button's reference. If you think the number of Buttons will remain constant during run-time, allocate the Buttons once, then invoke the configure method (see perldoc Tk::options) against each button to change its attributes.