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

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

I am referring to TK::Listbox http://search.cpan.org/dist/Tk/pod/Listbox.pod I have a Listbox:
$frames{"Account.List"} = $mw->Scrolled( "Listbox", -bg => $config{'background'}, -fg => '#FFFFFF', -font => [$config{'font'},$config{'fontsize'},'normal'], -relief => 'raised', -scrollbars => 'e', -cursor => 'left_ptr', );
And bounded <Double-1>:
$frames{"Account.List"}->bind( '<Double-1>', $frames{'Account.List'}->itemconfigure($frames{"Account.List"}->cu +rselection(), -foreground=> "#FF0000", );
Here is the question: How do I retrieve the foreground value #FF0000? Docs says to use itemconfigure without value: $frames{'Account.List'}->itemcget($frames{"Account.List"}->curselection(), "-foreground") but that does not work. I am stuck! My ultimate task for that is to: 1st. flip-flop the color of the text. 2nd: flip-flop the font from normal to bold. How? Thanks!

Replies are listed 'Best First'.
Re: How to get foreground color from a TK::Listbox
by Discipulus (Canon) on Apr 14, 2018 at 12:49 UTC
    Hello jsteng and welcome to the monastery and to the wonderful world of perl!

    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*

    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.
      :) Seems there is no solution for font flipflopping? Will work around this. thanks!
        Hi, the Listbox is a very useful widget, but has it's limitations. I would suggest making your own widget on a Tk::Canvas. The Canvas allows you to do almost anything you want. I made a little Canvas based demo widget module to show how easy it is.... see Tk-CanvasDirTree. It shows how to easily import a list and display it anyways you want. If you want full control over colors and fonts and bindings, the Tk::Canvas is the way to go. The only real trick is measuring your font size in pixels, which is easy to do. Make your own wheel! :-)
Re: How to get foreground color from a TK::Listbox
by zentara (Archbishop) on Apr 14, 2018 at 13:02 UTC
    Hi, without a full working code example, we can only guess. Try the 'active' tag.
    $listbox->get('active'); $listbox->itemconfigure('active', -background => 'yellow');

    I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: How to get foreground color from a ListBox
by Anonymous Monk on Apr 14, 2018 at 08:25 UTC
    There is no such thing as a ListBox. there is a Paw::Listbox... Guessing is hard i give up dont mind me