in reply to Dynamic Tk buttons from init file
#!/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"; }
|
|---|