Page 375 -
P. 375
create the slider
Use tkinter for everything else
The tkinter library has a graphical interface element called Scale that lives
to help you create a slider. Take a look at this example code and let’s see how
it works:
Create a tkinter DoubleVar variable. Like
IntVar and StringVar, the DoubleVar variable
stores a “magic" value, this time a float.
The tkinter Scale volume = DoubleVar() The slider can be linked
widget creates a volume_scale = Scale(app, to a “magic" variable.
slider. variable = volume, Specify the
resolution value,
Specify the lowest and
from_ = 0.0, which indicates the
the HIGHEST values on
to = 1.0, interval.
the scale.
resolution = 0.1,
Provide a nice, human- Connect the
command = change_volume, widget to an
friendly label for the
label = "Volume", event handler.
slider.
orient = HORIZONTAL)
volume_scale.pack(side = RIGHT)
Indicate whether the slider
runs across the screen
(HORIZONTAL) or up the
Click and drag screen (VERTICAL).
the slider to
adjust the volume.
The Scale() element is your most complex tkinter widget yet. But, despite
this, it is not hard to work out what’s going on here. The graphical interface
element is linked to a tkinter DoubleVar (called variable), the lowest/
highest slider values are provided (to and from_), and an interval between
them (resolution) is specified. The event handler is assocated with an
event handler (command), a descriptive label is supplied (label), and, finally,
the orientiation of the slider is specified (orient). There’s a lot going on
here, but none of it is that hard to understand.
340 Chapter 9