in reply to Perl/Tk and the "-variable => \$var" option
Well you will find in Tk, that it IS NOT ALWAYS AN ELEGANT LANGUAGE. Tk widgets are written and ported by different people, and sometimes what should work... dosn't. You could look at the Scale widget's source code and find out where the glitch is, but more often than not, you just want to find a hack to work around the problem.
There are a few widgets that I have noticed will NOT automatically update themselves from variables which are referenced(or from other module objects). I'm guessing here, but whomever wrote the scale widget, probably concentrated his effort to make it work in the other direction, that is, you set the slider, and the reference value is changed. Or he ignored arrays.
When I first ran your code, and saw it didn't respond, the first thing I did was manually make it update, like in the code below. Even $mw->update wouldn't do it. Also there is the '-command' option for the Scale widget, and quite often it is the key to automating a callback to update widgets. In the code below, I manually update the scale. In the readmore which follows, I show that the scale widgets dosn't like references in arrays, and works fine with straight scalar references. I didn't try a hash, instead of array, but I'll bet a hash fails too.
In this readmore, I show that Scale will work with plain scalar refs.#!/usr/bin/perl use warnings; use strict; use Tk; my @SLIDERVALUES = (0,0,0,0); my @slider; my $i; sub show_array { print "\@SLIDERVALUES = ($SLIDERVALUES[0], $SLIDERVALUES[1], $SLIDERVA +LUES[2], $SLIDERVALUES[3])\n"; } # window setup my $mw = MainWindow->new; foreach my $i (0 .. 3) { $slider[$i] = $mw->Scale( -from => 30, -to => 0, -orient => 'vertical', -variable => \$SLIDERVALUES[$i], # -command => undef, )->pack(-side => 'left'); } show_array(); # This breaks things! @SLIDERVALUES = (5, 10, 15, 20); show_array(); slider_update(); $SLIDERVALUES[0] = 7; $SLIDERVALUES[1] = 14; $SLIDERVALUES[2] = 21; $SLIDERVALUES[3] = 28; show_array(); slider_update(); $mw->update; MainLoop; ############################################# sub slider_update{ foreach my $i (0 .. 3) { $slider[$i]->set( $SLIDERVALUES[$i] ); } }
#!/usr/bin/perl use warnings; use strict; use Tk; my ($s1,$s2,$s3,$s4) =(0,0,0,0); my @slider; my $i; sub show_vals { print "SLIDERVALUES = $s1 $s2 $s3 $s4\n"; } # window setup my $mw = MainWindow->new; $slider[1] =$mw->Scale( -from => 30, -to => 0, -orient => 'vertical', -variable => \$s1, )->pack(-side => 'left'); $slider[2] = $mw->Scale( -from => 30, -to => 0, -orient => 'vertical', -variable => \$s2, )->pack(-side => 'left'); $slider[3] = $mw->Scale( -from => 30, -to => 0, -orient => 'vertical', -variable => \$s3, )->pack(-side => 'left'); $slider[4] = $mw->Scale( -from => 30, -to => 0, -orient => 'vertical', -variable => \$s4, )->pack(-side => 'left'); show_vals(); ($s1,$s2,$s3,$s4) = (5, 10, 15, 20); show_vals(); ($s1,$s2,$s3,$s4) = (7, 14, 21, 28); show_vals(); $mw->update; MainLoop;
|
|---|