I tried a bit and it seems i get trouble first time I query one property: $lb->itemcget($sel, '-background') so I provide the initial value manually: my $current_color_bg = $lb->itemcget($sel, '-background') || 'yellow';
Here a working example flipping the colors:
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( '<Double-1>', 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_b +g}, -fg=>$next_color{$current_color_fg} ); } ); MainLoop;
Double clicking the first entry 3 time and one time the second:
pressed 0 current color of the element 0 is yellow on red pressed 0 current color of the element 0 is red on yellow pressed 0 current color of the element 0 is yellow on red pressed 1 current color of the element 1 is yellow on red
L*
In reply to Re: How to get foreground color from a TK::Listbox
by Discipulus
in thread How to get foreground color from a TK::Listbox
by jsteng
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |