Home > Software engineering >  How to add loop in tkinter label
How to add loop in tkinter label

Time:09-17

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?

CodePudding user response:

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()

CodePudding user response:

add a condition in the program

  • Related