Tk::ItemStyle doesn't always create a new style when -stylename is specified
See this question on PerlMonks.
To reproduce:
#!/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, # <- comment this line out
-foreground => $color,
-background => 'Wheat',
-selectforeground => 'LightSeaGreen');
$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{$style});
}
MainLoop();
Sometimes needs to run several times to misbehave. Comment the indicated line out to get the correct behaviour.
See https://github.com/eserte/perl-tk/commit/bdc6bcc8c836e0b1dec87da54d34ab751d54a92d for a possible fix.
However, specifying the -stylename does not have much value in Perl, as we usually use the objects to reference things in Tk (unlike Tcl, where widgets and styles are referenced by names). So maybe the documentation should make clear that -stylename should not be used.
Thanks a lot! I think the fix is better than deprecating the -stylename. Old code still works (maybe even the way it was originally intended to). This was not a bug in the documentation, but in the implementation.