I am trying to develop a GUI in Tkinter. I have a main code
from menus import *
from Tkinter import *
from SpeedControl import *
Main = Tk()
Main.tk_setPalette(background='white')
GenerateMenus(Main)
GenerateSlider(Main)
mainloop()
The code for GenerateSlider is from Tkinter import *
def GenerateSlider(master):
display1 = Canvas(master, width=150, height=80, relief=RAISED, bd=5)
display1.grid(row=0,column=0)
display1.create_rectangle(10, 10, 150, 80, fill='dark red')
slider1 = Scale(master, from_=0, to=100, length=400,
tickinterval=10, orient=HORIZONTAL, relief=SUNKEN, bd=5,
bg='white', troughcolor='black', sliderrelief=RAISED)
slider1.grid(row=0,column=1)
display1.create_text(50,20, font='Helvatica 28', text=slider1.get(), fill='white', anchor=NW)
display2 = Canvas(master, width=150, height=80, relief=RAISED, bd=5)
display2.grid(row=1,column=0)
display2.create_rectangle(10, 10, 150, 80, fill='dark red')
slider2 = Scale(master, from_=0, to=100, length=400,
tickinterval=10, orient=HORIZONTAL, relief=SUNKEN, bd=5,
bg='white', troughcolor='black', sliderrelief=RAISED)
slider2.grid(row=1,column=1)
display2.create_text(50,20, font='Helvatica 24', text=slider2.get(), fill='white', anchor=NW)
What I am trying to do here is that the program should display the current value of the slider as text inside the canvas. The initial value is displayed in the canvas but I need a method to update the value when the slider moves.
Let me elaborate on my comment. This is what I mean (using classes, and a simplified example to suit my purpose):
Edit: Removed the lambda from the call to
updateCanvas
ascommand
for Scale widgets passes it's value when called. I was able to instead giveupdateCanvas
a parameter sliderVal which is automatically passed, and then I use that to update the text. Also removed the call toupdate()
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.