Display value of scale in Canvas in Tkinter

1.6k views Asked by At

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.

1

There are 1 answers

3
maccartm On

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.