One of the uses of the Slate is to rapidly create custom widgets. We'll use the Slate and some interactors to simulate a custom widget that mimics a "slider" as used on audio equipment. This example is a simple version of the complete Slider widget contained in the Slate directory.
First, make a clean slate:
set slate [::tycho::Slate::demoslate]
Now, create some items that represent the slider components. The
first is a text item to display the current value:
set value [$slate create text 50 20 -text 0 -anchor s -fill blue]
The second is the vertical "trough" of the slider:
set trough [$slate create Frame 48 23 52 143 -color darkgrey \
-borderwidth 2 -relief sunken]
The third is the horizontal "bar" of the slider:
set bar [$slate create Frame 40 132 60 142 \
-color darkseagreen -borderwidth 3]
When the bar is moved, we want to update the display of the slider
value. Assuming the slider ranges from 0 to 11, this procedure will
update it:
proc updatevalue {args} {
global slate bar value
set position [expr [lindex [$slate coords $bar] 1] + 5]
set x [expr (137.0-$position)/108.0 * 11.0]
$slate itemconfigure $value -text [format %.1f $x]
}
Now, we need some interactors to move the bar. Firstly, create and
bind a Bounder that moves the bar along the trough. It is given a
value for its -bounds
option to prevent it being moved past
the ends of the trough:
set bounder [$slate interactor Bounder \
-dragcommand updatevalue \
-constrain y -bounds {0 24 0 142}]
$bounder bind $bar -button 1
set stepper [$slate interactor Stepper -stepsize [expr 108.0/22] \
-dragcommand updatevalue]
$bounder cascade $stepper
$bounder uncascade
set label [$slate create text 50 150 -text "A slider" -anchor n \
-justify center]
set editor [$slate interactor TextItemEdit]
$editor bind $label
$editor stop
$editor unbind $label
::tycho::Displayer .d
::tycho::Slider .d.s -label Fred -resolution 0.5 -to 11 -command puts
pack .d.s
.d centerOnScreen
See the Slider User's Guide for a description of its operation, the Slider class documentation for more information on its calling interface and options, and the Interactor overview document for detailed information on the interactors.