Create a Tkinter window auto update without using "after()" function

Viewed 24

I'm using Tkinter for a small overlay on a screen that got to be update every 1 or 2 second. I search a lot about it and find the after() function that could be execute after the mainloop. But this one doesn't work quitely, the idea is to call a after() function witch contain another after() function and the main function that we want to execute in the loop, like that :

def my_functions():
    print('task done')
    ws.after(1000, my_functions)

ws.after(1000, my_functions())

But this one got a limit of "function calling itself" of 992, that means my window will refresh her value only for 16 min 32 sec (or 992 sec) then will crash. I could maybe destroy and recreate all my window and loop before the limit but I doesn't love this solution and I would have to work a lot on it, I would prefer a easyest solution, but I'm searching for 3 days and doesn't find anything good.

1 Answers

With trying to reproduce the following error : RecursionError: maximum recursion depth exceeded while calling a Python object I find the solution, so for any other I will write it. Like you said, you can't use the after() method if you pass an argument like that:

root.after(1000, task(arg))

If you want to pass an argument you have to do like that:

root = Tk()
x = 0
def task(x):
    x += 1
    print(x)
    root.after(2000, task, x)  # reschedule event in 2 seconds

root.after(2000, task, x)
root.mainloop()

If you use the first solution, it's only rise the error after the of the maximum recursion, tkinter doesn't find any mistake in the formulation and you can still execute your code. Which is a bit weird for me, but I just doesn't understand quitely the documentation when I read it. And with re-reading it seems logic now.

Related