in reply to Re: Change button color with Tk
in thread Change button color with Tk

I have some working code now, but my buttons are each a unique name. They are $button1 through $button28. Here's the code that works and the sub for the first button:
my $button1 = $mw->Button(-text => "18-Letters", -command => sub {print OUTFILE "18L\n"}, -command => \&change)->pack(-side => "top"); sub change { $button1->configure(-background=>"yellow") }
Aside from coding a sub for each button, could I use a if then to configure for an array? The goal is to change the color of the button once it is pressed.
my @btn = $button1-$button28;

Replies are listed 'Best First'.
Re^3: Change button color with Tk
by Discipulus (Canon) on Nov 02, 2017 at 20:14 UTC
    Hello PerlCowboy,

    you can use a brutal method like storing all buttons into an hash:

    my $mw = MainWindow->new; my %buttons; for (1..9){ $buttons{$_} = $mw->Button(-text=>"Button $_",-command=>[\& +colorize,$_])->pack; } MainLoop; sub colorize{ $buttons{$_[0]}->configure(-bg=>'yellow'); print $buttons{$_[0]}->cget('-text')," button is yellow n +ow\n"; }

    Or you can use Canvas tagging facility to select the current one

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.