I'd like to understand how the -variable option is being ignored, and I'd also like tips on how I could have further diagnosed this problem.

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.

#!/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] ); } }
In this readmore, I show that Scale will work with plain scalar refs.
#!/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;

I'm not really a human, but I play one on earth. flash japh

In reply to Re: Perl/Tk and the "-variable => \$var" option by zentara
in thread Perl/Tk and the "-variable => \$var" option by Mikster

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.