You are running into one of the limitations of using prebuilt widgets like the listbox. Highlighting selections have predefined meanings, and you just can't override them. The style of the 'active' element can be underlined or a dashed box around it. Color change is reserved for 'selected' items'. Look at this script, and remove the 'if entry press' conditional and see what happens..... the listbox needs a way to differentiate between the active and selected items, especially when the select mode is multiple. Now you can write your subclass of Listbox, and try to change the color definitions, but it is tricky because the mouse is also involved. You may want to look at the Tk::HList, which gives more control over the color schemes, with the 'style' options. In this example below, you may be able to selectionClear everything before setting the new selection, but then you lose multiple selection mode. It's tricky.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $mw=tkinit;
my @selects;
my $lb1 = $mw->Listbox->pack();
$lb1->insert('end',('A'..'J'));
$lb1->bind('<Enter>' => sub{ $lb1->focus });
$mw->bind('<KeyPress>' => sub {
$lb1->focus;
my $e = $lb1->XEvent; # get reference to X11 event struc
+ture
my $key = $e->K;
# comment out the if {} and see what happens
if( $key eq 'Return' ){
push @selects, $lb1->get('active');
$lb1->itemconfigure('active', -background => 'yell
+ow');
}
$mw->update;
print "@selects\n";
} );
MainLoop;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.