Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

How to get foreground color from a TK::Listbox

by jsteng (Beadle)
on Apr 14, 2018 at 04:40 UTC ( #1212850=perlquestion: print w/replies, xml ) Need Help??
jsteng has asked for the wisdom of the Perl Monks concerning the following question:

I am referring to TK::Listbox 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 (Monsignor) 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


    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1212850]
Approved by Corion
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-08-22 05:40 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (204 votes). Check out past polls.