Home > Blockchain >  tkinter scale slider value not showing
tkinter scale slider value not showing

Time:11-22

below is my code for a basic tkinter scale:

root = tk.Tk()
root.geometry('150x75')
root.resizable(width=False, height=False)
root.eval('tk::PlaceWindow . center')

v1 = tk.IntVar()

ttk.Scale(root, from_=0, to=100, orient='horizontal', variable=v1).pack(pady=20)

root.mainloop()

below is the attached output :

enter image description here

why is the slider value not showing above the slider ?

I would very much appreciate if someone could point out the error with the code.

CodePudding user response:

There is no error in your code. I think the answer is "because it's not designed to". There is no mention in the documentation of the ttk scale widget that it shows the value.

CodePudding user response:

The tkinter.ttk version of Scale does not support showing the value.

But the tkinter version of Scale DOES support showing the Scale value, by default. The showvalue option defaults to 1. You can set it to 0 to hide the value if so desired.

CodePudding user response:

Try something like this:-

import tkinter as tk
from tkinter import ttk


# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('Slider Demo')


root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=3)


# slider current value
current_value = tk.DoubleVar()


def get_current_value():
    return '{: .2f}'.format(current_value.get())


def slider_changed(event):
    value_label.configure(text=get_current_value())


# label for the slider
slider_label = ttk.Label(root, text='Slider:')

slider_label.grid(column=0, row=0, sticky='w')

#  slider
slider = ttk.Scale(root, from_=0, to=100, orient='horizontal', command=slider_changed, variable=current_value)

slider.grid(column=1, row=0, sticky='we')

# current value label
current_value_label = ttk.Label(root, text='Current Value:')

current_value_label.grid(row=1, columnspan=2, sticky='n', ipadx=10, ipady=10)

# value label
value_label = ttk.Label(root,text=get_current_value())

value_label.grid(row=2, columnspan=2, sticky='n')


root.mainloop()

Link to site from which I got the answer:- https://www.pythontutorial.net/tkinter/tkinter-slider/

  • Related