Scale

The scale widget is useful when there are many options within a range that are possible which would make a listbox unwieldly. What is the current temperature outside? What percentage of the hard disc is used? The basic syntax is
my $scale = $mw->Scale( [ option => value ] );
Some basic options are As well, there is a method We give two examples. The first one,
#!perl
# file scale.pl
use Tk;
use strict;
use warnings;
my $scale_value = 'not given';
my $message = "Your age is $scale_value";
my $mw = MainWindow->new;
$mw->title('Scale');
my $label = $mw->Label(-textvariable => \$message);
my $age = $mw->Scale(-label => 'Age',
                     -orient => 'vertical',
                     -from => 10,
                     -to => 90,
                     -resolution => 10,
                     -tickinterval => 20);
my $show = $mw->Button(-text => 'Show age',
                       -command => \&display);
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$label->pack;
$age->pack;
$show->pack;
$exit->pack;
MainLoop;
sub display {
  $scale_value = $age->get();
  $message = "Your age is $scale_value";
}
is pictured below.

Figure 3.11: Example of a scale widget
Image scale

Here the $show button, when clicked, will invoke the display subroutine. This routine will update the $message string with the current value of the scale, accessed through the $age_scale->get() method. The $message string is subsequently used as the text for the label $label.

The second example,

#!perl
# file scale1.pl
use Tk;
use strict;
use warnings;
my $scale_value = '10';
my $message = "Your age is $scale_value";
my $mw = MainWindow->new;
$mw->title('Scale');
my $label = $mw->Label(-textvariable => \$message);
my $age = $mw->Scale(-label => 'Age',
                     -variable => \$scale_value,
                     -orient => 'horizontal',
                     -from => 10,
                     -to => 90,
                     -resolution => 10,
                     -tickinterval => 20,
                     -command => \&display);
my $exit = $mw->Button(-text => 'Exit',
                      -command => [$mw => 'destroy']);
$label->pack;
$age->pack;
$exit->pack;
MainLoop;
sub display {
  $message = "Your age is $scale_value";
}
is illustrated below.

Figure 3.12: Another example of a scale widget
Image scale1

In this example the display routine is called through the -command option to the scale $scale. Subsequently, the $message string, again used as the text for the $label label, will be updated with each change of the value associated with the scale. Note that, if the callback associated with -command is complicated, this can slow your application down significantly, as the callback is invoked for every change in the scale.

Randy Kobes 2003-11-17