當我單擊它呼叫的 cpuTemp 函式的按鈕時,它有一個后回圈初始化,導致我的視窗沒有回應,它在我的 python 控制臺中顯示 cpu 百分比的值,所以問題是為什么它不作業'''
from tkinter import *
import psutil
import statistics
window = Tk()
# window size
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
# window
window.attributes('-transparentcolor', 'blue')
window.resizable(True, True)
window.attributes('-alpha', 0.96)
window.config(cursor='crosshair')
window.attributes('-topmost', 0)
window.geometry(f"{screen_width}x{screen_height} 20 20")
window.state('zoomed')
window.title('Hello Python')
# windTemp
def cpuTemp(event):
#gettin temps
cpuTemps = [psutil.cpu_percent(0.5), psutil.cpu_percent(0.5), psutil.cpu_percent(0.5),
psutil.cpu_percent(0.5), psutil.cpu_percent(0.5)]
meanVal = statistics.mean(cpuTemps)
print(meanVal)
lbl.configure(text=f'{meanVal}%')
window.after(1000 , cpuTemp(event))
#button
btn = Button(window, text="This is Button widget", fg='blue')
btn.place(x=80, y=100)
btn.bind('<Button-1>', cpuTemp)
#label
lbl = Label(window , text='hi' , fg = "#0009ff0fc")
lblPlace = [ screen_width/2 , screen_height/2]
lbl.place(x=f'{lblPlace[0]}', y=f'{lblPlace[1]}')
window.mainloop()
# temp
this is not working can anyone fix this for me I would appreciate that.
it stills print in my pycharm consloe so why is my window not responding.
i am using pycharm as you might know now.
and I want to make this code working .
i am a python newbie so pls help it would mean a lot to me...
uj5u.com熱心網友回復:
window.after(1000 , cpuTemp(event)) 立即運行cpuTemp(event)并將結果傳遞給window.after. 這會創建一個無限回圈,因為每次呼叫都會導致對該函式的另一次呼叫。
代碼需要看起來像這樣:
window.after(1000, cpuTemp, None)
原因None是該函式不使用事件,event除了處理原始事件時,當前相對無用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/429386.html
下一篇:如何從函式回傳多個值?
