Python Tkinter Scale Widget
In this post, you will learn about the Python Tkinter scale widget.
Python provides various libraries for developing Graphical User Interfaces (GUIs) applications, such as PyQT, Tkinter, Kivy, and PySide. Tkinter is the standard GUI library that provides a fast and easy way to create GUI applications. It is also called the Tk interface. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. The greatest strength of Tkinter is its straightforwardness.
Python Tkinter provides a Scale widget to create a slider. It allows a user to select a numeric value by moving a slider knob along a scale. We can arrange the slider horizontally or vertically. The Scale widget creates a graphical object that allows a user to set a value by moving an indicator.
Syntax of scale() widget
scale(parent, options,...)
Here, the word parent is used to represent the parent window. These are the lists of options for the scale widget.
Option |
Description |
activebackground | To set the color of the slider when the mouse is over the scale. |
bg | To set the background color of the parts of the widget that are outside of the trough. |
bd | To set the 3-d border around the slider. |
command | The procedure is called every time the slider is moved. |
cursor | To change the cursor icon when the mouse is over the scale. |
digits | A control variable to read the current value shown in a scale widget. It can be an IntVar, DoubleVar, or a StringVar. |
font | The font type used for labels or annotations. |
fg | To set the foreground color of the text. |
from_ | It contains one end of the scale. This is the top end of the vertical scale and the left end of the horizontal scale. |
highlightbackground | To set the color of the focus highlight when the widget does not have focus. |
highlightcolor | To set the color of the focus highlight when the widget has focus. |
highlightthickness | To set the thickness of the focus highlight. |
label | To set a label within the scale widget. |
length | The length of the scale widget. By default, they are 100 pixels. |
orient | Set orient = HORIZONTAL if you want the scale to run along the x direction. |
relief | To set a border around the label. |
repeatdelay | To set the duration up to which the button is pressed before the slider starts moving in that direction repeatedly. |
resolution | This is the smallest unit that the user will be able to change the scale in whole units. |
showvalue | To show the current value of the scale in text form via the slider. Set this option to 0 to suppress that label. |
state | By default, the scale widget is activate. We can set this value to 'DISABLED' for making it unresponsive. |
takefocus | The focus will cycle through the scale widgets by default. We can set this option to 0. |
tickinterval | No ticks are displayed by default. We can set this option to a periodic number. |
to | It contains one end of the scale. This is the bottom end for the vertical scale and the right end for the horizontal scale. |
troughcolor | To set the color of the trough. |
variable | It shows a control variable for the scale. |
width | The width of the trough part of the widget. |
Methods of scale widget
- get() - This method returns the current value of the scale.
- set(value) - This method sets the scale's value.
Simple tkinter scale widget Program
Here is the basic scale slider program -
from tkinter import *
master = Tk()
w = Scale(master, from_=0, to=50)
w.pack()
w = Scale(master, from_=0, to=50, orient=HORIZONTAL)
w.pack()
mainloop()
Output of the above code-

Tkinter scale get value
In the given tkinter scale widget program, we are getting the current value of the scale.
from tkinter import *
def getValue():
selection = "Value = " + str(var.get())
label.config(text = selection)
root = Tk()
var = DoubleVar()
scale = Scale( root, variable = var )
scale.pack(anchor=CENTER)
button = Button(root, text="Get Scale Value", command=getValue)
button.pack(anchor=CENTER)
label = Label(root)
label.pack()
root.mainloop()
Output of the above code:

Advance Tkinter Scale Program
Here, we have added more options to the scale widget for enhancing the features of the scroller. We have defined 'onScroll()' method that will be called every time the slider is moved and we have created a button that is attached with 'readScale()' method to read the current slider value.
from tkinter import *
class TkSlider(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.pack()
self.var = IntVar()
Scale(self,label='Miles',
command=self.onScroll,
variable = self.var,
from_=0 , to=100,
length=200,
tickinterval=20,
orient=HORIZONTAL,
bg="#8ED2C9",
fg="black",
troughcolor="#006495",
width=20 ).pack()
Button(self , text='Read',
command=self.readScale).pack(pady=10)
def onScroll(self, value):
print ('Current Value = ' , value)
def readScale(self):
print ('Read Scroller = ' , self.var.get())
if __name__ == '__main__' :
TkSlider().mainloop()
Output of the above code-

Related Articles
Python OpenCV Overlaying or Blending Two ImagesInstall NLTK for Python on Windows 64 bit
Python OpenCV Histogram of Grayscale Image
Find the stop words in nltk Python
Python Tkinter Combobox Event Binding
Python Tkinter Combobox
Add background image in Python Tkinter
Python Tkinter Text Widget
Countdown clock and timer using Tkinter in Python
Python Tkinter Frame Widget
Python Tkinter Checkbutton Widget
Entry Field Validation in Tkinter Python
Python Tkinter Tutorial with Examples
Python Tkinter Scale Widget
Python3 Tkinter Messagebox
Python 3 Tkinter Menu Bar
Python Tkinter Geometry Managers
Python get visitor information by IP address
Python OpenCV ColorMap
Python Webbrowser
Python Pandas Plotting
Python Pandas CSV to Dataframe