Display value of scale in Canvas in Tkinter
Let me elaborate on my comment. This is what I mean (using classes, and a simplified example to suit my purpose):
from Tkinter import *class Window(): def __init__(self, master): self.display1 = Canvas(master, width=150, height=80, relief=RAISED, bd=5) self.display1.grid(row=0,column=0) self.display1.create_rectangle(10, 10, 150, 80, fill='dark red') self.slider1 = Scale(master, from_=0, to=100, length=400, tickinterval=10, orient=HORIZONTAL, relief=SUNKEN, bd=5, bg='white', troughcolor='black', sliderrelief=RAISED, command = self.updateCanvas) self.slider1.grid(row=0,column=1) self.createdText = self.display1.create_text(50, 20, font = 'Helvatica 28', text = self.slider1.get(), fill = 'white', anchor = NW) def updateCanvas(self, sliderVal): self.display1.itemconfig(self.createdText, text = sliderVal)master = Tk()w = Window(master)master.mainloop()
Edit: Removed the lambda from the call to updateCanvas
as command
for Scale widgets passes it's value when called. I was able to instead give updateCanvas
a parameter sliderVal which is automatically passed, and then I use that to update the text. Also removed the call to update()
as it was unnecessary.
Edit 2: Updated the code to reflect the suggestion by fhdrsdg, which allows the text to be updated without needing to destroy & recreate it every time the update function is called.