In this Python tutorial, we will learn about the Python Tkinter Scale which is also known as the Python Tkinter Slide. Also, we will cover these topics:
- Python Tkinter scale widget
- Python Tkinter scale-up dpi
- Python Tkinter scale window
- Python Tkinter scale command
- Python Tkinter scale command=lambda
- Python Tkinter scale default value
- Python Tkinter scale get value
Python Tkinter scale widget
Scale widget in Python Tkinter is used to create a scale with a slide on the application. This widget is used in the applications where some measurement is required and the common applications that use the python Tkinter scale are:
- Microsoft Word
- WordPad
- drawing tools
- Volume Mixer
The below image shows the use of the scale widget in the volume mixer desktop application which is present in all the versions of the Microsoft Windows 10 operating system.
Python Tkinter scale can be oriented in vertical or horizontal format. By default, it is set to vertical orientation.
Syntax:- Below is the syntax to create a scale widget in Python Tkinter.
Scale(
master=ws,
from_=0,
to=200,
orient=HORIZONTAL
).pack(side=BOTTOM)
Parameters in the syntax:-
- master=ws: parent or child window name
- from_: starting value for the slider
- to: end value for the slider
- orient: orientation can be horizontal or vertical
- pack() : pack is the geometry manager
Also, check: Python Number Guessing Game
Python Tkinter scale-up dpi
While creating an application using Python Tkinter if you are observing a blur effect on the widgets then you can improve the resolution quality by altering the dpi (dot per inch) value.
Python’s built-in library ‘ctypes’ provides a function SetProcessDpiAwareness() which improves the quality of screen resolution in Python Tkinter.
Using the Python Tkinter scale widget we can create a GUI-based interactive window that will take user input to set the dpi values.
Example:-
In the below example, we have created a function scaleup(val), and this function trigger SetProcessDpiAwareness() function with an argument.
You can fetch the values of slider or scale using the get function in Python Tkinter. We have passed scale.get() as an argument for SetProcessDpiAwareness function. Now, whatever the user selects the scale value, the same will be assigned to the function.
Please Note– scaleup(val), where val is just an argument that you need to pass to make this function work.
# modules
from tkinter import *
import ctypes
# window
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
# function
def scaleup(val):
ctypes.windll.shcore.SetProcessDpiAwareness(scale.get())
# scale widget
scale = Scale(
ws,
from_=0,
to=1,
length=200,
orient=HORIZONTAL,
command=scaleup,
font=('sans-serif', 14)
)
scale.pack()
# other widgets
Label(ws, text='PythonGuides', font=('sans-serif', 20), pady=20).pack()
Button(ws, text='Click Here!', font=('sans-serif', 14)).pack()
# infinite loop
ws.mainloop()
Output:-
In the below output, there are two images labeled 1 and 2. The first image shows the default output where the value of SetProcessDpiAwareness() function is set to 0.
In the next image, SetProcessDpiAwareness() function value is set to 1 and you can observe the improvement in the resolution of the windows.
Read: Python Tkinter Filter Function()
Python Tkinter scale window
Scale window means changing the size of the Tkinter window when a user slides the scale widget up and down or right and left.
The geometry method in a window determines the size of the Tkinter window and it requires mandatory parameters such as width and height other than this you can also provide the x and y coordinates to position the application.
Below is the syntax to configure the geometry of the window in Python Tkinter:
ws.geometry('width x height + x + y')
In this syntax, all the text is a string and there are operator signs (x, +). A similar thing we have to do with the scale widget.
Scale value will be fetched using the get() function and then these values will be converted into strings with operator signs. You can also use formatted strings to perform these actions.
Example:-
In the below example, we have created two windows (parent and child) and a slider is placed on the parent window. Users can change the size of the child window using the slider.
The scalewin(val) is the function that will alter the window size of the win, which is a child window of ws.
# modules
from tkinter import *
# function
def scalewin(val):
win.geometry(str(scale.get()) + 'x' + str(scale.get()))
# window
ws = Tk()
ws.title('PythonGuides')
# child window
win = Toplevel(ws)
win.geometry()
# scale widget
scale = Scale(
ws,
from_= 100,
to=1000,
command=scalewin
)
scale.pack()
# infinite loop
ws.mainloop()
Output:-
In this output, the size of the window is changing as the slider on the scale widget is moved in the upward or downward position.
Read: Python Tkinter after method
Python Tkinter scale command
The command option in the scale widget is used to trigger a function. It could be a normal function or an anonymous function.
In this section, we will learn the use of the command option for scale widgets in Python Tkinter using normal functions.
Example:-
In our example, we have created a function that will return the slider position in Python Tkinter. Every time user moves the slider on the scale a function is triggered that will display a message on the window.
Here is the overview of the scale widget used in our final example in Python Tkinter:
scale = Scale(
master = ws,
from_ = 0,
to=20,
length=600,
tickinterval=2,
orient=HORIZONTAL,
command = disp_val
)
scale.pack()
- master=ws : widget is placed on the window named as ws
- from_=0 : scale starts from 0
- to=20 : scale ends at 20
- length=600 : length of the scale widget
- tickinterval=2: scale values will appear with the multiplication of 2
- orient=HORIZONTAL: orientation can be horizontal or vertical
- command = disp_val: disp_val is the name of the function that will be triggered every time slider is moved on the scale.
Here is an example of a Python Tkinter scale command:
# module
from tkinter import *
# function
def disp_val(val):
var.set(f'Slider is at {scale.get()}')
# create window
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#93AEBF')
# scale widget
scale = Scale(
master = ws,
from_ = 0,
to=20,
length=600,
tickinterval=2,
orient=HORIZONTAL,
command = disp_val
)
scale.pack()
# variable with integer values only
var = IntVar()
# label widget to display selected number
Label(
ws,
textvariable=var,
font=('Times New Roman', 16)
).pack(side=BOTTOM)
# infinite loop
ws.mainloop()
Output:-
In the below output, when a user slides the scale widget, the selected number is displayed at the bottom of the window. The number is updated with the change in the slider value.
Read: Python Tkinter messagebox
Python tkinter scale command=lambda
Lambda function is an anonymous function which means it can be written in a single line and is mainly used for single time tasks.
Learn More: Python Anonymous function (lambda function)
In our example, we have mentioned the below code to create Python Tkinter scale command=lambda.
command=lambda val : var.set(scale.get())
Code snippet explanation:
- command: it is the option in the scale widget that triggers a function.
- lambda val: here val is the argument, although it is not used in the program but the code won’t work without an argument.
- var.set(): it will set the value to the label, here var is representing the label.
- scale.get(): this command will get the selected value from the scale widget.
Here is the implementation of a complete example:-
# modules
from tkinter import *
# window
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#93AEBF')
# scale widget
scale = Scale(
master = ws,
from_ = 0,
to=20,
length=600,
tickinterval=2,
orient=HORIZONTAL,
command=lambda val : var.set(scale.get())
)
scale.pack()
# variable with integer value only
var = IntVar()
# label widget to display output
Label(
ws,
textvariable=var,
font=('Times New Roman', 16)
).pack(side=BOTTOM)
# infinite loop
ws.mainloop()
Output:-
The output is similar to the previous section by this time execution is done using the lambda function in Python Tkinter.
Read: Python Tkinter Spinbox
Python Tkinter scale default value
Python Tkinter scale widget allows creating a scale on the Tkinter widget with a slider on it. Users can slide right-left or up-down depending upon the orientation of the window.
Python Tkinter scale default value means every time the program will be executed it will display a specific value. By default, the scale sets starting value as the default value but it can be changed using the set() function on the scale variable.
# module
from tkinter import *
# window
ws = Tk()
ws.title('PythonGuides')
ws.geometry('600x200')
# variable with integer values only
var = IntVar()
# set scale default value as 50
var.set(50)
# scale widget
Scale(
ws,
label='Scale',
tickinterval=5,
variable=var,
from_=10,
to=100,
length=500,
orient=HORIZONTAL
).pack()
# infinite loop
ws.mainloop()
Output:-
Since we have set the default value of the scale widget as 50 so every time the program is executed the slider will be placed on the 50 by default.
Read: Python Tkinter search box
Python Tkinter scale get value
Get method in Python Tkinter scale is used to fetch the value for the scale. It will return the current value on which the slider is currently placed.
After creating a scale or slider it is meaningless if the program does not use the value selected by the user to perform some action.
Example:- In this example, we have created an application that will display the current position of the slider on the scale. For this, we have used Scale, Label, and button widgets.
Using scale.get() we are fetching the value on the current position of the slider and based upon that information we are assigning background color to the
from tkinter import *
def showval(val):
if scale.get() == 1:
ws.config(bg='#7AB3BF')
elif scale.get() == 2:
ws.config(bg='#868C54')
elif scale.get() == 3:
ws.config(bg='#F2EFEB')
elif scale.get() == 4:
ws.config(bg='#8C7484')
elif scale.get() == 5:
ws.config(bg='#0487D9')
elif scale.get() == 6:
ws.config(bg )
else:
ws.config(bg='#BF4C41')
ws = Tk()
ws.title('PythonGuides')
ws.geometry('300x200')
scale = Scale(
ws,
from_=0,
to=10,
length=200,
orient=HORIZONTAL,
command=showval
)
scale.pack(side=BOTTOM)
ws.mainloop()
Output:-
In this output, the background color of the window changes with the change in the slider value on the scale widget.
You may also like to read the following Python Tkinter tutorials.
- Python Tkinter On-Off Switch
- Python Tkinter notebook Widget
- Python Tkinter Multiple Windows
- Python Tkinter Image + Examples
- Python Tkinter Separator
In this tutorial, we have learned about the Python Tkinter Scale which is also known as the Python Tkinter Slide. Also, have covered these topics:
- Python Tkinter scale
- python Tkinter scale-up dpi
- Python Tkinter scale window
- Python Tkinter scale widget
- Python Tkinter scale command
- Python tkinter scale command=lambda
- Python Tkinter scale default value
- Python Tkinter scale get value
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.