tk6. Scale in Tkinter

A scale can be used to select a number within a range. Here the range is from 1 to 10.

The number selected is printed in the label just underneath it, using the command function of the Scale.

Whenever the command is invoked by a scale change, a parameter is passed by Tkinter. It is referred to as e here, but it does not make any difference what we call it, as long as we are consistent.


# tk6.py
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.geometry('400x300')
root.title('Scale')
root['bg']='blue'
scaleVal = StringVar()
scaleVal.set('5.000')
scale = Scale(root, from_=1, to=10,
orient = HORIZONTAL,
command = lambda e: scaleVal.set('%.3f' % float(e)))
scale.set(5)
scale.pack(fill = X)

label = Label(root, textvariable = scaleVal, pad = 100)
label.pack(expand = True)
root.mainloop()

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *