pythonloopsuser-interfacetkinterpsutil

How to add loop in tkinter label


import  psutil
import time
from tkinter import *


root = Tk()
Label = Label(root, text=psutil.cpu_percent()).pack()
time.sleep(1)
root.mainloop()

Here's the code. Now I want to refresh the psutil.cpu_percent() after 1 sec continuously. How can I make such a loop in it?


Solution

  • I didn't test it. Try this.

    from tkinter import *
    import  psutil
    
    root = Tk()
    def cpu_percent():
        Label(root, text=psutil.cpu_percent()).pack()
        root.after(1000, cpu_percent)
    
    root.after(1000, cpu_percent)    
    root.mainloop()