我正在開發一個監控計算機溫度的 python tkinter 程式,我希望它在固定時間后更新溫度值。以下功能是我曾經這樣做的:
def update():
get_temp()#the function that get the computer temperature value, like cpu temperature.
...
def upd():
update()
time.sleep(0.3)
upd()#recursive call function.
upd()
但是這種方式會達到遞回限制,所以程式會在一段時間后停止。我希望它不斷更新值,我該怎么辦?不知道改了after()會不會好點。但是如果我使用after(),tkinter 視窗會凍結一段時間,所以我不想使用它。謝謝你。
uj5u.com熱心網友回復:
在這個用例中遞回是不夠的,請改用回圈。
Tkinter in particular has got a method which allows you to execute a function in an interval without disrupting the GUI's event loop.
Quick example:
from tkinter import *
root = Tk()
INTERVAL = 1000 # in milliseconds
def get_temp()
# ...
root.after(INTERVAL, get_temp)
get_temp()
root.mainloop()
uj5u.com熱心網友回復:
It needs loop. It should be:
def update():
get_temp()#the function that get the computer temperature value, like cpu temperature.
...
def upd():
while True:#needs a while true here and don't call upd() in this function.
update()
time.sleep(0.3)
upd()#this upd() is outside the function upd(),it used to start the function.
Thanks to everyone who helped me.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431749.html
下一篇:如何在CMD中顯示錯誤輸入?
