Display scale value in canvas in Tkinter

I am trying to develop a GUI in Tkinter. I have main code

from menus import *
from Tkinter import *
from SpeedControl import *

Main = Tk()
Main.tk_setPalette(background='white')
GenerateMenus(Main)
GenerateSlider(Main)
mainloop()

      

The GenerateSlider code is from a 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'm 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 I move the slider.

+3


source to share


1 answer


Let me elaborate on my comment. This is what I mean (using classes and a simplified example for 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 lambda from call to updateCanvas

as command

for Scale widget passes its value when called. I was able to instead give updateCanvas

a sliderVal parameter that gets passed automatically and then I use that to update the text. The challenge was also removed update()

as it was optional.

Edit 2 . Updated code to reflect fhdrsdg's suggestion that allows text to be updated without having to destroy and recreate it every time the update function is called.

+1


source







All Articles