Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How to get foreground color from a TK::Listbox

by Discipulus (Abbot)
on Apr 14, 2018 at 12:49 UTC ( #1212869=note: print w/replies, xml ) Need Help??


in reply to How to get foreground color from a TK::Listbox

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.

Replies are listed 'Best First'.
Re^2: How to get foreground color from a TK::Listbox
by jsteng (Beadle) on Apr 14, 2018 at 14:12 UTC
    :) 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! :-)
        OK! Seems like a good place to finally solve my project.
        Will work on a canvas solution and see how that goes from there on. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1212869]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2020-10-25 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?