Skip to content
Advertisement

How to remove scale value from tkinter Scale widget display?

I am trying to use a tkinter Scale widget in an application and whenever I set the tickinterval attribute, a value is displayed below the slider.

Is there a way to suppress the display of scale values below the slider and still specify the tickinterval?

My use case is to create a non-linear slider where scale values 0-6 map to a list of values like 1, 4, 10, 40, 100, 400, 1000. In this case it makes no sense to display the widget’s values to the user.

My other alternative is to use a combobox or a series of radio buttons.

Suggestions are welcome.

Here is an example program:

import tkinter as tk

def scaleChanged():
   scaleValue = "Value = " + str(var.get())
   label.config(text = scaleValue)

root = tk.Tk()
root.geometry('300x150+100+100')
var = tk.DoubleVar()
scale = tk.Scale( root, orient=tk.HORIZONTAL, from_=0, to=6, showvalue=0,
                  variable = var, length=180, tickinterval=30)
scale.pack(anchor = tk.CENTER, pady=(20,10))

button = tk.Button(root, text = "Fetch Scale Value", command = scaleChanged)
button.pack(anchor = tk.CENTER, pady=10)

label = tk.Label(root)
label.pack()

root.mainloop()

Output

Advertisement

Answer

You can set the tickinterval=0 (or remove it) so that it will not display values below the slider. Then when you are getting the value of the slider in the button, use a mapping of 0:1… 6:1000 and display it in the scale window.

Below is the complete script:

import tkinter as tk

def scaleChanged():
   mapping={0:1, 1:4, 2:10, 3:40, 4:100, 5:400, 6:1000}
   scaleValue = "Value = " + str(mapping.get(var.get()))
   label.config(text = scaleValue)


root = tk.Tk()
root.geometry('300x150+100+100')
var = tk.DoubleVar()
scale = tk.Scale( root, orient=tk.HORIZONTAL, from_=0, to=6, showvalue=0,
                  variable = var, length=180, tickinterval=0)
scale.pack(anchor = tk.CENTER, pady=(20,10))

button = tk.Button(root, text = "Fetch Scale Value", command = scaleChanged)
button.pack(anchor = tk.CENTER, pady=10)

label = tk.Label(root)
label.pack()

root.mainloop()

Sample output:

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement