my understanding of Perl/Tk is very limited, so I don't really know if
this is a bug or an error on my side.
The attached program shows some weird behaviour. When starting and
pressing 'p' I get
$VAR1 = {
'1' => 'alpha',
'3' => 'gamma',
'2' => 'beta'
};
which is OK. Now, when I select "3" and change the value to "omega" I get
$VAR1 = {
'1' => 'omega',
'3' => 'omega',
'2' => 'beta'
};
after pressing 'p'. This, of course is not the behaviour I intended.
Playing around it seems to me, that the first association of the
Optionmenu's -textvariable seems to be retained in addition to the new
value from the callback.
Could someone more knowledgeable shed some light on what's going on?
Thanks,
Oliver
#!/usr/bin/perl
use Tk;
use Data::Dumper;
my %categories = ( "1" => "alpha",
"2" => "beta",
"3" => "gamma");
my @subcategories = ("alpha", "beta", "gamma",
"tau", "theta", "omega");
my $mw = MainWindow->new();
my $lb = $mw->Listbox(
-listvariable => [ sort keys(%categories) ])->pack();
my $selection;
tie $selection, "Tk::Listbox", $lb;
$lb->selectionSet(0);
my $om = $mw->Optionmenu(
-options => \@subcategories,
-textvariable => \$categories{@$selection[0]}
)->pack();
$lb->bind('<ButtonPress>' => sub {
$om->configure(
-textvariable => \$categories{@$selection[0]});
});
$mw->bind('<KeyPress-p>' => sub {
print Dumper(\%categories);
});
MainLoop();
When are you expecting \$categories{@$selection[0]})
to be evaluated?
>> my $om = $mw->Optionmenu(
>> -options => \@subcategories,
>> -textvariable => \$categories{@$selection[0]} )->pack();
>>
>> $lb->bind('<ButtonPress>' => sub {
>> $om->configure(
>> -textvariable => \$categories{@$selection[0]});
>> });
>
> When are you expecting \$categories{@$selection[0]}) to be evaluated?
Hmm, your remark doesn't help me.
\$categories{@$selection[0]} is evaluated the first time, when
the Optionmenu widget is created, giving the address in memory of
$categories{'1'}.
Then every time I press a mouse button in the Listbox. $selection is
set correctly before the binded subroutine is called. textvariable then
points to the correct place in memory.
Someone correct me, if I'm wrong.
A workaround is to include
-variable => \$categories{@$selection[0]},
in the configure call inside the callback.
I still don't understand what's going on.
Oliver