How to add loop in tkinter label

Question:

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?

Asked By: Jahin Ahnaf

||

Answers:

add a condition in the program

Answered By: Umair Ejaz

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()
Answered By: toyota Supra