New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Python Tkinter Scale Widget

In this tutorial, we will cover the Tkinter Scale widget in Python which is used to add a graphical slider object which the user can slide and choose a number, as a numeric value is attached to this slider scale and as you move the slider up/down or right/left the numeric value attached to it increases or decreases and you can set the slider to the value you wish to select.

  • The sliding bar provided by the scale widget is helpful in selecting the values just by sliding from left to right or top to bottom depending upon the orientation of the sliding bar in our application.

  • The scale widget is used as an alternative to the Entry widget if the purpose of the Entry widget is to take numeric input from user within a given range of values.

  • You can also control minimum and maximum values along with the resolution of the scale.

Tkinter Scale Widget

The syntax of the Tkinter Scale widget is given below:

W = Scale(master, options)   

In the above syntax, the master parameter denotes the parent window. You can use many options to change the layout of the scale widget and these options are written as comma-separated key-values.

Tkinter Scale Widget Options:

Following are the various options used with Tkinter Scale widget:

Name of the option Description
activebackground This option represents the background color of the widget when it is under focus.
bg This option represents the background color of the widget
bd This option represents the border size of the widget. The default value is 2 pixels.
cursor With the help of this option, the mouse pointer will be changed to a specific cursor type and it can be an arrow, dot, etc.
command This option will be set to the procedure which is called every time when we move the slider. If we move the slider rapidly, the callback to the procedure is done when it settles.
digits When the control variable which is used to control the scale data is of string type, then this option is mainly used to specify the number of digits when the numeric scale is converted to a string.
fg This option indicates the foreground color of the text
font This option indicates the font type of the text
from_ This option is used to represent one end of the widget range.
highlightcolor This option indicates the highlight color when the widget is under the focus
highlightbackground This option indicates the highlight color when the widget is not under the focus
label This option can be set to some text which then can be shown as a label with the scale. If the scale is horizontal then it is shown in the top left corner or if the scale is vertical then it shown in the top right corner.
length This option indicates the length of the widget. It represents the X dimension if the scale is in the horizontal direction and it represents the Y dimension if the scale is in a vertical direction.
relief This option is used to specify the border type. Its default value is FLAT
orient This option can be set to either horizontal or vertical depending upon the type of the scale.
resolution This option will be set to the smallest change which is made to the value of the scale
repeatdelay This option is mainly used to tell the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly. its default value is 300 ms
sliderlength This option represents the length of the slider window along the length of the scale. Its default value is 30 pixels. Also, you can change it to the appropriate value.
showvalue By default, the value of the scale is shown in the text form, also we can set this option to 0 in order to suppress the label.
state By default, the state of the scale widget is active. To make it unresponsive you can also set it to DISABLED
width This option is used to represent the width of the trough part of the widget
variable This option is used to represent the control variable for the scale
to This option is used represents a float or integer value that specifies the other end of the range represented by the scale
takefocus Generally, the focus will cycle through the scale widgets. If you don't want this behavior you can set this option to 0.
tickinterval With the help of this option, scale values are displayed on the multiple of the specified tick interval. The default value of this option is 0.
troughcolor This option is used to set the color for the trough

Tkinter Scale Widget Methods

Following are the few methods used with Scale widgets:

  • get():

    This method is used to get the current value of the scale.

  • set(value):

    This method is used to set the value of the scale.

Tkinter Scale Widget - Horizontal Example

Below we have a basic example where we will create a horizontal slide bar.

from tkinter import *  

win = Tk()  
win.geometry("200x100")
  
v = DoubleVar()  

scale = Scale( win, variable=v, from_=1, to=50, orient=HORIZONTAL)  
scale.pack(anchor=CENTER)  
  
btn = Button(win, text="Value")  
btn.pack(anchor=CENTER)  
  
label = Label(win)  
label.pack()  
  
win.mainloop()


Tkinter scale widget example

In this tutorial, we have created a horizontal Scale widget. If you see in the code, we have specified the orient as HORIZONTAL for this. We have also specified the range of numeric values for the slider scale.

Tkinter Scale Widget - Vertical Example

Below we have another example where we will create a vertical slider:

from tkinter import *

win = Tk() 
win.geometry("400x300") 

v = DoubleVar() 

def show(): 	
	sel = "The Vertical Scale Value is = " + str(v.get()) 
    # adding scale value to label to show
	scale_val.config(text=sel, font=("Courier", 16)) 

scl = Scale(win, variable=v, from_=60, to=1, orient=VERTICAL) 

mainlabel = Label(win, text="The Vertical Slider") 

btn = Button(win, text ="Show Slider Value", 
			command = show, 
			bg = "darkblue", 
			fg = "white") 

# creating another label to show the scale value
scale_val = Label(win) 

scl.pack(anchor = CENTER) 
mainlabel.pack() 
btn.pack() 
scale_val.pack() 

win.mainloop() 


Tkinter vertical scale widget slider example

You can move the slider from bottom to top as it is a vertical slider. In the given example, we have also added a button to our application, and we have defined a function show() that is attached to the button widget as an event handler. So after the user uses the slider to select any value, and then clicks on the button, the value will be displayed in a Label widget below the button.

Summary:

In this tutorial, we learned about the Tkinter Scale widget which is a good UI component for taking numerical input value within a specific range from the end user.