http://qs321.pair.com?node_id=1104672

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

hi all, i want to create an array of buttons and handle them when clicked. in my code the last button in the array is handled even if any other button is clicked. pls help. thanks

my @buttons; for my $i(0..5){ push @buttons, $mw->Button(-text => $i, -command => sub{ print "pressed $i\n"; # or use the widget: print "pressed ", $buttons[$i]->cget('-text'), "\n"; })->pack; }

Replies are listed 'Best First'.
Re: Array of buttons
by choroba (Cardinal) on Oct 22, 2014 at 11:55 UTC
    What GUI are you using? Works correctly for me in Tk.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Array of buttons
by ww (Archbishop) on Oct 22, 2014 at 13:42 UTC

    Using Win7's CLI, prints the graphic as indicated, and prints "pressed n" to the CLI (NOT to the TK $mw) when a numbered button is clicked upon.

    YMMV if using xwindows or similar but my *nix boxen are comatose right now.


    ++$anecdote ne $data


Re: Array of buttons
by glenn (Scribe) on Oct 22, 2014 at 13:53 UTC
    Yes, a 'print' will go to STDOUT. So what are you trying to accomplish?