2015-04-21 231 views
2

這是我的代碼和子功能被執行,但它沒有得到mainloop()。 如果我註釋掉「update_Lux(labelLuxValue)」,窗口就會出現。 我想不通爲什麼:(Tkinter:窗口無法打開

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() 
+0

可能calcLux()會花費太多時間?如果你將100增加到2000或更多,會發生什麼? – paddyg

回答

2

你在你的兩個方法update_Luxupdate_CT無限循環。

此行

label.after(100, update_Lux(label)) 

應該

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

label.after(100, update_Lux, label) 

否則,你的update_Lux功能路過不要after,但結果 ...而當該方法被調用,它再次嘗試後,等等等等傳遞結果。

1

好吧,那很好。 我現在有另一個問題。 還有另一個叫做「update_CT」的函數。結構是一樣的。 兩個順序都不起作用。

其中既包括像一個 「更新」 功能:

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

作品雖然:)

但他們爲什麼不工作單獨在一起嗎?