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

I've noticed that I'm getting inconsistent colours (colors).

I've tracked this down to ItemStyle returning inconsistent hashes (in reality, references to hashes).

Inconsistent in that:

The code below amply demonstrates the problem:

#!/usr/bin/perl use Tk; use Tk::ItemStyle; $mw = Tk::MainWindow->new(); $TkStyle{Black } = $mw -> ItemStyle("text", -stylename=>"Black", + -foreground=>"Black", -background=>"Wheat", -selectforegrou +nd=>"LightSeaGreen"); $TkStyle{LightGrey } = $mw -> ItemStyle("text", -stylename=>"LightGrey +", -foreground=>"grey60", -background=>"Wheat", -selectforegrou +nd=>"LightSeaGreen"); $TkStyle{Red } = $mw -> ItemStyle("text", -stylename=>"Red", + -foreground=>"Red", -background=>"Wheat", -selectforegrou +nd=>"LightSeaGreen"); $TkStyle{Blue } = $mw -> ItemStyle("text", -stylename=>"Blue", + -foreground=>"Blue", -background=>"Wheat", -selectforegrou +nd=>"LightSeaGreen"); $TkStyle{RoyalBlue1} = $mw -> ItemStyle("text", -stylename=>"RoyalBlue +1", -foreground=>"RoyalBlue1", -background=>"Wheat", -selectforegrou +nd=>"LightSeaGreen"); $TkStyle{RoyalBlue3} = $mw -> ItemStyle("text", -stylename=>"RoyalBlue +3", -foreground=>"RoyalBlue3", -background=>"Wheat", -selectforegrou +nd=>"LightSeaGreen"); $TkStyle{Green } = $mw -> ItemStyle("text", -stylename=>"Green", + -foreground=>"ForestGreen", -background=>"Wheat", -selectforegrou +nd=>"LightSeaGreen"); $TkStyle{Orange } = $mw -> ItemStyle("text", -stylename=>"Orange", + -foreground=>"Orange", -background=>"Wheat", -selectforegrou +nd=>"LightSeaGreen"); foreach $colour (keys %TkStyle) { $style = $TkStyle{$colour}; $count{$style}++; } foreach $colour (keys %TkStyle ) { $style = $TkStyle{$colour}; printf("%10s has count %d (style=$style)\n", $colour, $count{$sty +le}); }
There's no MainLoop ... just so that the program exits immediately.

A typical output may be:

RoyalBlue3 has count 3 (style=Tk::ItemStyle=HASH(0x556aa72eb408))

Orange has count 1 (style=Tk::ItemStyle=HASH(0x556aa72eb4b0))

RoyalBlue1 has count 1 (style=Tk::ItemStyle=HASH(0x556aa72e61d0))

Red has count 3 (style=Tk::ItemStyle=HASH(0x556aa72eb408))

Blue has count 3 (style=Tk::ItemStyle=HASH(0x556aa72eb408))

Green has count 2 (style=Tk::ItemStyle=HASH(0x556aa72e6128))

LightGrey has count 1 (style=Tk::ItemStyle=HASH(0x556aa72e6188))

Black has count 2 (style=Tk::ItemStyle=HASH(0x556aa72e6128))

This does seem very curious and ... wrong?

Running the script can return very different failure signatures.

Occasionally, all the references to hashes are unique ... but that seems to be more the exception than the norm.

I have added a loop (not shown) to the creation of the ItemStyle's ... so that duplicated hashes are deleted and new ones created.

Sometimes, I have to iterate 3 or even 4 times until I get a completely unique set of hashes.

Any thoughts?

Andrew

Replies are listed 'Best First'.
Re: ItemStyle not returning a unique hash (Updated)
by choroba (Cardinal) on Apr 08, 2025 at 09:34 UTC
    I can confirm the bug. It sometimes manifests even with just 2 existing styles. Adding a MainLoop also shows that the styles are overwriting each other, i.e. the items have wrong colours.

    A full example rewritten to use strict to rule out typos in variable names, plus shortened according to the DRY principle.

    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::ItemStyle; use Tk::TList; my $mw = Tk::MainWindow->new(); my $tl = $mw->TList->pack; my %TkStyle; my %count; for my $color (qw( Black Red Green Blue )) { $TkStyle{$color} = $mw->ItemStyle('text', -stylename => $color, -foreground => $color, -background => 'Wheat', -selectforeground => 'LightSeaGr +een'); $tl->insert('end', -itemtype => 'text', -text => $color, -style => $TkStyle{$color}); ++$count{ $TkStyle{$color} }; } for my $colour (keys %TkStyle ) { my $style = $TkStyle{$colour}; printf("%10s has count %d (style=$style)\n", $colour, $count{$styl +e}); } MainLoop();

    Update: The large Tk application I maintain at work uses ItemStyle, but I've never encountered the bug. I checked the code and it seems the only difference is our application doesn't set the -stylename: And indeed, after commenting out the -stylename line, the hashes are always unique!

    Update 2: Reported.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Thank you for your quick reply.

      I can confirm that removing -stylename does seem to work.

      That is fab :)