Chapter 10. The Scale Widget
A Scale
widget looks similar to a Scrollbar but doesn't scroll anything
other than itself. What it does is keep track of a number. When you
change the position of the button in the Scale, the numeric value
associated with the Scale changes. Some things you can do with a
Scale widget are:
-
Create a widget from which a user can select a number between 1 and
100.
-
Create three Scales, each
representing a value in an RGB (red, green, blue) number.
-
Create four sliders, each representing a
portion of an IP address. Each Scale can go from 0 to 255, and it
would probably be smart to start them at 255. Use a Label widget to
show the completed IP address, periods and all.
-
Create a temperature Scale that starts at -50 and goes to 130 degrees.
-
Show the amount of rainfall so far this year. The Scale can be marked
to show every five inches.
The Scale widget can be placed horizontally or vertically, depending
on where you have the most room in your application window. Figure 10-1 shows a scale widget.
Figure 10-1. Scale widget
10.1. Creating a Scale
As with
other widgets, to create a Scale, use a parent widget and pass
options to the Scale to change its configuration:
$parent->Scale( [ option => value ] )->pack;
Use one of the geometry managers discussed in Chapter 2, "Geometry Management" to place it on the screen (such as
pack, as shown in the preceding code).
Most of the options associated with the Scale widget are standard
options used with all other widgets. All the possible options are in
the following list. A discussion of special options that have
slightly different meanings for the Scale and options that are
specific to the Scale widget follows the list.
- -activebackground => color
-
Sets the color the slider's
background should be when the cursor is over the slider
(-state is 'active').
- -background => color
-
Sets the color the slider's
background should be when the cursor is not over the slider
(-state is 'normal').
- -bigincrement => amount
-
Sets the amount by which the slider will change value when required
to do so in large increments. Default is 0, causing the value to
change by 1/10 the top value of the Scale.
-bigincrement is used only when
Control-Up/Down/Left/Right is pressed.
- -borderwidth => amount
-
Sets the width of the edges of the
widget. Default is 2.
- -command => callback
-
Sets the callback invoked when the slider
is moved.
- -cursor => cursorname
-
Determines the cursor to display when the
mouse is over the Scale.
- -digits => amount
-
Indicates how
many significant digits to retain when conversion from a number to a
string takes place.
- -font => fontname
-
Sets the font used to display any text in
the Scale.
- -foreground => color
-
Sets the color of the text
displayed in the Scale.
- -from => value
-
Indicates the low end of the Scale values.
Default is 0.
- -highlightbackground => color
-
Sets the color of the highlight rectangle
displayed around the Scale when it does not have the keyboard focus.
- -highlightcolor => color
-
Sets the color of the highlight
rectangle displayed around the Scale when it has the keyboard focus.
- -highlightthickness => amount
-
Sets the thickness of the highlight
rectangle displayed around the Scale.
- -label => labelstring
-
Describes a label for the Scale. Default
is no label.
- -length => amount
-
Sets the length of the slider (the long
direction, regardless of the value of -orient) in
a valid screen distance.
- -orient => 'vertical' | 'horizontal'
-
Sets the direction the Scale is drawn.
- -relief => 'raised'|'sunken'|'flat'|'ridge'|'groove'|'solid'
-
Determines how the edges of the widget are
drawn.
- -repeatdelay => milliseconds
-
Sets the number of milliseconds
the widget waits before repeating.
- -repeatinterval => milliseconds
-
Sets the number of milliseconds
the widget delays between repeats.
- -resolution => value
-
Sets the increments by which the
value in the Scale will change. Default is 1.
- -showvalue => 0 | 1
-
If set to
0, the value of the slider setting is not shown at all.
- -sliderlength => value
-
Sets the size of the slider
(inside the widget). Default is 25.
- -state => 'normal' | 'active' | 'disabled'
-
Determines the state of the widget and whether or not the user can
interact with it.
- -takefocus => 1 | 0 | undef
-
Determines whether or not the
widget can receive keyboard focus. Default is to let the program
decide.
- -tickinterval => value
-
Describes the labels drawn by the right
(or on the bottom) of the Scale. Labels are drawn for every value
except 0 (which means no labels will be drawn at all). Default is 0.
- -to => value
-
Sets the top value of the Scale. Default
is 100.
- -troughcolor=> color
-
Sets the
color of the area behind the slider button (same as a Scrollbar).
- -variable => \$variable
-
Sets the variable in which the
slider value is stored.
- -width => amount
-
Sets the width of the skinny part of the
slider (regardless of the value associated with
-orient).
| | | 9.21. A Drawing Program Example | | 10.2. Assigning a Callback |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|
|