Using the Tkinter Scale Widget

Tkinter Scale

Hello everyone! In this part of our Tkinter Tutorial, we shall cover yet another widget – the Tkinter Scale widget.

This is a useful widget to have, if you want to display a scale of any kind on your application.

We’ll demonstrate how you can use this widget, using a simple example. Let’s get started!


Basic Tkinter Scale Widget Options

We can use this widget when we want the user to enter a numerical value, bounded within certain limits.

The user can move across the scale and select the appropriate value for the input.

To use this, the basic options are of the following format:

scale_widget = tk.Scale(master, from_, to, orient)

Here, master is the Tkinter master object, which is necessary for any Tkinter GUI application to run.

The from_ and to keyword arguments specify the range of the slider values.

Finally, the orientation orient can be either “horizontal” or “vertical”.

There is one more optional argument called resolution, which specifies the numerical resolution of the values.

For example, the default resolution is 1, which will ensure that all values are rounded to the nearest integer. You can try tweaking with this parameter as per your need.

To use the current value of the Tkinter scale widget, you can simply call scale_widget.get(). And to set the Tkinter scale widget, you can call scale_widget.set(value).

Let’s now look at an example to illustrate what this means.


Using the Tkinter Scale Widget – Build a Scale for our Application

Let’s build a horizontal scale for our dummy application, which consists of integer values from 0 to 100.

This means that the numeric resolution is 1.

import tkinter as tk
from tkinter import messagebox

# Create the master object
master = tk.Tk()

# Create a Scale Widget
scale_widget = tk.Scale(master, orient="horizontal", resolution=1,
                        from_=0, to=100)

# And a label for it
label_1 = tk.Label(master, text="Scale")

# Use the grid geometry manager to put the widgets in the respective position
label_1.grid(row=0, column=0)
scale_widget.grid(row=0, column=1)

# The application mainloop
tk.mainloop()

Output

Scale Example 1
Scale Example 1

Alright! This works as expected, and we have our Tkinter scale widget displaying values from 0 to 100.

Let’s now add a Button widget to our application.

When we click on the button after setting the scale value, we’ll invoke a function call to output a Message Box alert, which will display what value we chose.

Let’s add the Button widget to our program.

button = tk.Button(master, text="Click", command=buttonCallback)
button.grid(row=1, column=1)

Here, the command argument specifies the function that will get called when this button gets clicked.

We will call a function called buttonCallback, which handles this event for us.

def buttonCallback():
    global scale_widget
    messagebox.showinfo("Message", "You have chosen value {}".format(scale_widget.get()))

I’m using the scale_widget on the global scope, so that we can get the value using scale_widget.get(). This value will get printed on a message box.

As an add on, I’ll also increase the default frame size from the standard “200×200”, to a larger “300×300” window, using master.geometry().

master.geometry("300x300")

I’ll post the complete code now.

import tkinter as tk
from tkinter import messagebox

# Create the master object
master = tk.Tk()

master.geometry("300x300")

# Create a Scale Widget
scale_widget = tk.Scale(master, orient="horizontal", resolution=1,
                        from_=0, to=100)

# And a label for it
label_1 = tk.Label(master, text="Scale")

# Use the grid geometry manager to put the widgets in the respective position
label_1.grid(row=0, column=0)
scale_widget.grid(row=0, column=1)

def buttonCallback():
    global scale_widget
    messagebox.showinfo("Message", "You have chosen value {}".format(scale_widget.get()))

button = tk.Button(master, text="Click", command=buttonCallback)
button.grid(row=1, column=1)

# The application mainloop
tk.mainloop()

Now, let’s look at the output.

Tkinter Scale Example
Tkinter Scale Example

Indeed, when you click on the Button, after setting the scale value, the correct number is displayed!


Conclusion

In this tutorial, we looked at using the Tkinter Scale widget.

In the upcoming articles, we’ll cover a few more widgets, so stay tuned for more!