Home > Net >  .after() method in tkinter only gets called once
.after() method in tkinter only gets called once

Time:04-24

I'm trying to make an infinite loop using the Tkinter root.after() method. idealy the loop function should be called every second. But the function in the second argument of after is only getting called once. Am I missing something concerning .after or is .after the wrong approach on to begin with?

import tkinter as tk
root=tk.Tk()
def loop():
  print("hello World")
root.after(1000, loop)
root.mainloop()

This code only prints "hello World" once instead of the desired call every second

CodePudding user response:

import tkinter as tk
root=tk.Tk()


def loop():
    print("hello World")
    # once 'loop()' is called from root, it will get called again here
    after(1000, loop)


root.after(1000, loop)  # initial call to start 'loop()'
root.mainloop()
  • Related