Home > Back-end >  Python recursive limit and how to keep monitoring something
Python recursive limit and how to keep monitoring something

Time:02-24

I am working on a python tkinter program that monitoring computer temperature, and I want it to update the temperature value after a fixed time. the following function is what I used to do that:

    def update():
        get_temp()#the function that get the computer temperature value, like cpu temperature.
        ...
    def upd():
        update()
        time.sleep(0.3)
        upd()#recursive call function.

    upd()

but this way will hit the recursive limit, so the program will stops after a period of time. I want it to keep updating the value, what should I do? I don't know if I change it to after() it will be better or not. but if I use after(), the tkinter window will freeze a while, so I don't want to use it. Thank you.

CodePudding user response:

Recursion is inadequate in this use-case, use a loop instead.

Tkinter in particular has got a method which allows you to execute a function in an interval without disrupting the GUI's event loop.

Quick example:

from tkinter import *

root = Tk()
INTERVAL = 1000 # in milliseconds

def get_temp()
   # ...
   root.after(INTERVAL, get_temp) 

get_temp()
root.mainloop()
  • Related