is it possible to run a function on a timed loop in tkinter with the tkinter window already displayed?

36 Views Asked by At

I'm trying to make a tkinter window that displayes then routinely updates the visuals by running a function every few seconds. currently the solutions I have found only display the screen after the functions have run thousands of times without any delay dispite .after(ms,function) being used

import tkinter
from tkinter import *

top = tkinter.Tk()
top.geometry("500x500")
top.title("display screen test")


global reps

def functiontorepeat():
    global reps
    reps = reps + 1
    labelexample.config(text = reps)
    top.after(5000,functiontorepeat())
    #why does this not have a delay when run
    #and why does this only show the screen after the reps hits a recursion depth limmit


labelexample = Label(top,text = "original text")
labelexample.pack(side = TOP)
reps = 0
functiontorepeat()


top.mainloop()
1

There are 1 best solutions below

0
Module_art On

Difference between calling and passing a function.

top.after(5000,functiontorepeat()) will call the function immediately
But you need to pass the function rather than call it.
top.after(5000,functiontorepeat) without brackets!

import tkinter
from tkinter import *

top = tkinter.Tk()
top.geometry("500x500")
top.title("display screen test")


global reps

def functiontorepeat():
    global reps
    reps = reps + 1
    labelexample.config(text = reps)
    top.after(5000,functiontorepeat)

labelexample = Label(top,text = "original text")
labelexample.pack(side = TOP)
reps = 0
functiontorepeat()


top.mainloop()