use strict; use warnings; use Tk; my $mw = MainWindow->new; my %next_color=(red=>'yellow',yellow=>'red'); my $lb = $mw->Scrolled( "Listbox", -bg => 'yellow', -fg => 'red', -scrollbars => 'e', )->pack; $lb->insert('end', "Item1", "Item2", "Item3"); $lb->bind( '', sub{ my $sel = $lb->curselection()->[0]; print "pressed $sel\n"; my $current_color_bg = $lb->itemcget($sel, '-background') || 'yellow'; my $current_color_fg = $lb->itemcget($sel, '-foreground') || 'red'; print "current color of the element $sel is $current_color_bg on $current_color_fg\n"; $lb->itemconfigure( $sel, -background=> $next_color{$current_color_bg}, -fg=>$next_color{$current_color_fg} ); } ); MainLoop;