Kreatronik
Kreatronik

Reputation: 199

tkinter: Dynamic label to show value from sensor

The following code is what i have right now. I also tried it with the given solutions in the interweb like here Link1.

But this seems too complicated for a simple function like mine, i want to have more "elegant" code. That's what i have:

.
.
box = Tk()
box.title('TCS3490')
box.geometry('200x180')

labelLux = Label(master=box, text='Lux=')
labelLux.place(x=5, y=5, width=60, height=30)

labelLuxValue = Label(master=box, text=str(dev.calcLux()))
labelLuxValue.place(x=50, y=50, width=100, height=30)
labelLuxValue.after(10, labelLuxValue.config(text = str(dev.calcLux()))  

box.mainloop()

It puts a value once in that label but not again. dev.calcLux() returns a float value Why?

Upvotes: 0

Views: 1443

Answers (1)

Eric Levieil
Eric Levieil

Reputation: 3574

The problem in your code is that the after line is executed just once, you need a recursive function:

def update():
    labelLuxValue.config(text = str(dev.calcLux())
    labelLuxValue.after(100, update)

update() 

See for example: https://mail.python.org/pipermail/tkinter-discuss/2006-April/000704.html

Upvotes: 1

Related Questions