in reply to Dynamic Tk buttons from init file

Here is one solution. Let the user pass in:
  1. A hash uses button text as key, and coderef as value
  2. An array defines the sequence of user buttons (help the hash to maintain order)
Also we use an array to store the button handlers.

#!/usr/bin/perl use Tk; use strict; use constant BUTTON_WIDTH => 20; my $val; my %user_buttons = ("button1" => \&user_button1_func, "button2" => \&user_button2_func, "button3" => \&user_button3_func); my @user_button_sequence = ("button1", "button2", "button3"); my @user_buttons; my $mw = new MainWindow; my $entry = $mw->Entry(textvariable => \$val) ->pack(fill => "x"); my $default_button1 = $mw->Button(text => "Default Button 1", width => BUTTON_WIDTH, command => sub {$val = "default butt +on 1 clicked"}) ->pack(); my $default_button2 = $mw->Button(text => "Default Button 2", width => BUTTON_WIDTH, command => sub {$val = "default butt +on 2 clicked"}) ->pack(); foreach my $button_text (@user_button_sequence) { push @user_buttons, $mw->Button(text => $button_text, width => BUTTON_WIDTH, command => $user_buttons{$button_text}) ->pack(); } MainLoop; sub user_button1_func { $val = "user button 1 clicked"; } sub user_button2_func { $val = "user button 2 clicked"; } sub user_button3_func { $val = "user button 3 clicked"; }