user19222567
user19222567

Reputation:

tkinter call a function every second?

I want to call an update function every 1 second to update some stuff. I have tried using a while loop and time.sleep but that does not work. I though that I could attach a function to mainloop so it is called everytime Tk() updates. But I don't know any other way can you help?

from tkinter import *

def Update():
    #Stuff

Master = Tk()

Master.mainloop() # Whenever this updates call Update

Upvotes: 2

Views: 2336

Answers (1)

JRiggles
JRiggles

Reputation: 6820

Call the update function, and use after inside of update to have it call itself in order to repeat indefinitely

import tkinter as tk

master = tk.Tk()


def update():
    # do things
    master.after(1000, update)  # call update again after 1 second


update()  # begin updates
master.mainloop()

Upvotes: 3

Related Questions