Kreatronik
Kreatronik

Reputation: 199

Tkinter: window doesn't open

That's my code and the subfunction is executed, but it doesn't get to mainloop(). If i comment out "update_Lux(labelLuxValue)" the window shows up. I can't figure out why :(

from Tkinter import *

def update_Lux(label):
    label.config(text = str(dev.calcLux()))
    label.after(100, update_Lux(label))

def update_CT():
    labelCTValue.config(text = str(dev.calcCT()))
    labelCTValue.after(100, update_CT())

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

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

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

labelLuxValue = Label(master=box)
labelLuxValue.place(x=50, y=5, width=100, height=30)

labelCTValue = Label(master=box)
labelCTValue.place(x=50, y=30, width=100, height=30)

update_Lux(labelLuxValue)

box.mainloop()

Upvotes: 2

Views: 868

Answers (2)

Kreatronik
Kreatronik

Reputation: 199

Ok, that worked well. I have another problem now. There is another function called "update_CT". The structure is the same. Both in sequence won't work.

Including both in one "update" function like:

def update_LuxCT():
    labelLuxValue.config(text = str(dev.calcLux()))
    labelCTValue.config(text = str(dev.calcCT()))
    labelLuxValue.after(100, lambda:update_LuxCT())

works though :)

But why won't they work separately together?

Upvotes: 1

tobias_k
tobias_k

Reputation: 82949

You have infinite loops in your two methods update_Lux and update_CT.

This line

label.after(100, update_Lux(label))

should be

label.after(100, lambda: update_Lux(label))

or

label.after(100, update_Lux, label)

Otherwise you are passing not the update_Lux function to after, but the result of update_Lux(label)... and when that method is called, it again tries to pass the result to after, and so on and so on.

Upvotes: 2

Related Questions