import psutil
import time
from tkinter import *
def eventc(event):
while True :
print(psutil.cpu_stats())
time.sleep(2)
def closewin(event):
exit(0)
def myevent(event):
print('okayy')
window=Tk()
#button for stats
btn=Button(window, text="click me for cpu stats", fg='blue')
btn.place(x=80, y=100)
btn.bind('<Button>',eventc)
#btn for closing the window
winclose=Button(window, text="close window", fg='blue')
winclose.place(x=95, y=0)
winclose.bind('<Button>',closewin)
#for window
window.title('my \'so called first app\'')
window.geometry("300x200 10 10")
window.mainloop()
When I click get cpu stats is gives me stats but the window will not respond after that. and after forcefully ending the window it give me this exit code:Process finished with exit code -805306369 (0xCFFFFFFF) can you help me why
CodePudding user response:
The while loop inside eventc()
will block tkinter mainloop
from handling pending events and updates. Suggest to use .after()
to replace the while loop:
def eventc(event=None):
print(psutil.cpu_stats())
window.after(2000, eventc) # execute eventc() 2000ms later