tkinter scale slider value not showing
Question:
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 :
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.
Answers:
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.
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/
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.
You may try removing this from tkinter ttk import *
if you have imported something like this at the top of your code. Simply importing this from tkinter import *
helps in showing the values of the scale in slider.
Happy Coding!
A simple like this:
import tkinter as tk
from tkinter import ttk
from tkinter import *
root = tk.Tk()
root.geometry('150x75')
root.resizable(width=False, height=False)
root.eval('tk::PlaceWindow . center')
def sel():
selection = f"Value = str{var.get()}"
label.config(text = selection)
var = tk.DoubleVar()
scale = tk.Scale(root, variable=var )
scale.pack(anchor=CENTER)
label = Label(root)
label.pack()
root.mainloop()
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 :
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.
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.
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/
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.
You may try removing this from tkinter ttk import *
if you have imported something like this at the top of your code. Simply importing this from tkinter import *
helps in showing the values of the scale in slider.
Happy Coding!
A simple like this:
import tkinter as tk
from tkinter import ttk
from tkinter import *
root = tk.Tk()
root.geometry('150x75')
root.resizable(width=False, height=False)
root.eval('tk::PlaceWindow . center')
def sel():
selection = f"Value = str{var.get()}"
label.config(text = selection)
var = tk.DoubleVar()
scale = tk.Scale(root, variable=var )
scale.pack(anchor=CENTER)
label = Label(root)
label.pack()
root.mainloop()