Python 3 – Tkinter Scale
Tkinter is a standard Python library used in creating and designing graphical user interfaces(GUI) for desktop applications. It’s an open-source package that provides a set of tools for building windows with complex functionality in a few lines of code. One of the most popular GUI controls that is used for adjusting numeric values is the Scale widget. In this article, we’ll explain how to use the Scale widget in Tkinter, its methods, and its properties.
Importing the Tkinter Module
In this tutorial, we’ll use Python 3 as our programming language. To use Tkinter, we need to import the module by adding the following code at the beginning of our program:
import tkinter as tk
Creating a Scale Widget
To create a Scale widget, we use the Scale method provided by Tkinter. Here’s an example:
root = tk.Tk()
scale = tk.Scale(root, from_=0, to=10, orient=tk.HORIZONTAL)
scale.pack()
root.mainloop()
The code above creates a Scale widget with a range from 0 to 10, displayed horizontally. The orient parameter can be set to VERTICAL
for a vertical display.
Configuring the Scale Widget
There are several parameters that can be set while creating a Scale widget. Here are the most frequently used ones:
- from_: sets the minimum value of the scale.
- to: sets the maximum value of the scale.
- length: sets the length of the Scale widget in pixels.
- label: sets the text displayed next to the Scale widget.
- showvalue: specifies if the current value of the Scale widget is displayed.
- resolution: sets the increment between values.
Here’s an example of how to use these parameters:
root = tk.Tk()
scale = tk.Scale(root, from_=0, to=10, length=200, label="Range", showvalue=True, resolution=0.1)
scale.pack()
root.mainloop()
In the code above, we set the length of the Scale widget to 200, and the resolution to 0.1, which means that the values between each step will be 0.1.
Getting the Value of the Scale Widget
We can get the value of the Scale widget using the get method. Here’s how:
root = tk.Tk()
scale = tk.Scale(root, from_=0, to=10)
scale.pack()
root.mainloop()
value = scale.get()
print(value)
The code above creates a Scale widget, and when we move the slider, the value will be shown on the screen. The get method can be called to obtain the current value of the Scale widget.
Setting the Value of the Scale Widget
We can set the value of the Scale widget using the set method. Here’s an example:
root = tk.Tk()
scale = tk.Scale(root, from_=0, to=10)
scale.pack()
root.mainloop()
scale.set(5)
The above code creates a Scale widget and sets the initial value of the widget to 5.
Listening to Value Changes in the Scale Widget
If we want to perform an action based on the changing value of the Scale widget, we can use the command
parameter.
root = tk.Tk()
def scale_callback(value):
print("The Scale value is: ", value)
scale = tk.Scale(root, from_=0, to=100, command=scale_callback)
scale.pack()
root.mainloop()
In the code above, we defined a function called scale_callback
that will be called each time the Scale widget value changes. The print statement in the function will display the current value of the Scale widget.
Conclusion
This article has provided a brief introduction to the Scale widget in Tkinter, how to create it, set its properties, and how to get and set its value. The Scale widget is a powerful GUI control that enables users to adjust numeric values with ease. With this knowledge, you can now start building your Scale widget-based applications with Tkinter in Python.