Home > Software engineering >  How to remove scale value from tkinter Scale widget display?
How to remove scale value from tkinter Scale widget display?

Time:09-03

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

CodePudding user response:

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

CodePudding user response:

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

No, there is not. The documented way to turn off the tick values is to set tickinterval to zero. From the official documentation:

tickinterval: Must be a real value. Determines the spacing between numerical tick marks displayed below or to the left of the slider. The values will all be displayed with the same number of decimal places, which will be enough to ensure they are all accurate to within 20% of a tick interval. If 0, no tick marks will be displayed.

  • Related