Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Tk::Scale, how to avoid callback when displayed?

by perltux (Beadle)
on Dec 25, 2012 at 09:58 UTC ( #1010255=perlquestion: print w/ replies, xml ) Need Help??
perltux has asked for the wisdom of the Perl Monks concerning the following question:

To my surprise I found out that the Tk::Scale widget invokes the callback command when the widget is displayed for the first time, before any user interaction.

Is there any way to avoid that?

I would like the Tk::Scale widget to only invoke the callback command when the value is being modified, just like all other widgets behave.

Comment on Tk::Scale, how to avoid callback when displayed?
Re: Tk::Scale, how to avoid callback when displayed?
by Khen1950fx (Canon) on Dec 25, 2012 at 10:44 UTC
    That's normal behavior because the callback is called when the widget is first created and when each value is changed. The trick is to keep the callback down to the fewest possible values; hence the value can be modified by user interaction just like other widgets. Using an example from the source:
    #!/usr/bin/perl use strict; use warnings; use Tk; my $top = MainWindow->new(); $top->title("scale tests"); my $f = $top->Frame( -relief => 'ridge', -borderwidth => 3, ); my $s = $top->Scale( "-orient" => "horizontal", "-length" => 300, "-from" => 0, "-to" => 250, "-tickinterval" => 25, "-command" => sub { my $s; $f->configure( '-width' => $s->get ) } ); $s->pack(-side => 'left', -fill => 'y'); $f->pack(-side => 'right', -fill => 'y'); Tk::MainLoop;
      I don't quite understand the example code and how it relates to my problem. I tried running it but even your example invokes the command when the scale is first displayed before any user interaction (which is what I would like to avoid).
        Hi perltux,

        Would it be satisfactory to simply ignore the first call?

        Using the example Khen1950fx gave (and fixing the scoping issue with $s) you could do it like this:

        #!/usr/bin/perl use strict; use warnings; use Tk; my $top = MainWindow->new(); $top->title("scale tests"); my $f = $top->Frame( -relief => 'ridge', -borderwidth => 3, ); my $s; $s = $top->Scale( "-orient" => "horizontal", "-length" => 300, "-from" => 0, "-to" => 250, "-tickinterval" => 25, "-command" => sub { use feature 'state'; state $ncalls++ or return; printf "Callback: Scale(%d)\n", $_[0]; $f->configure( '-width' => $s->get ) } ); $s->pack(-side => 'left', -fill => 'y'); $f->pack(-side => 'right', -fill => 'y'); Tk::MainLoop;
        say  substr+lc crypt(qw $i3 SI$),4,5
Re: Tk::Scale, how to avoid callback when displayed?
by Anonymous Monk on Dec 25, 2012 at 23:02 UTC
    Are you sure about that (can you prove it)?
Re: Tk::Scale, how to avoid callback when displayed? (its a bug)
by Anonymous Monk on Dec 26, 2012 at 00:21 UTC

    That is a bug in Tk::Scale, -from/-to should be initialized before the -command is bound, you should report it :)

    It is easy to avoid if you don't use -command but use -variable instead :)

      I think you are right that it's a bug or at the very least a design flaw. Problem is I need and use both -variable and -command (-variable to store the data and -command to trigger an action when the data changes).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010255]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-08-02 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (55 votes), past polls