0

How can I call update periodically? I tried the following but it skips showing GUI for the limit seconds and then shows only the last update:

import tkinter as tk
import time

root = tk.Tk()

widget = tk.Label(root, text="Initial text")
widget.pack()

def update():
    global widget
    limit = 3
    period = 1
    for each in range(limit):
        widget['text'] = each
        time.sleep(period)

update()

root.mainloop()

Then I tried:

import tkinter as tk
import time

root = tk.Tk()

widget = tk.Label(root, text="Initial text")
widget.pack()

def update():
    global widget, period
    widget['text'] = each
    time.sleep(period)

limit = 3
period = 1
for each in range(limit):
    update()

root.mainloop()

Which resulted the exact same way as the former. So how can I do this?

Nae
  • 14,209
  • 7
  • 52
  • 79
  • Also see [this question & answers](https://stackoverflow.com/q/459083/7032856). – Nae Dec 11 '17 at 17:33

1 Answers1

1

Instead of time.sleep try using after in the following way as it won't delay your GUI to show:

import tkinter as tk


def update():
    global each, limit, period, widget
    if each < limit:
        widget['text'] = each
        each += 1
        widget.after(period*1000, update)

root = tk.Tk()

widget = tk.Label(root, text="Initial text")
widget.pack()


limit = 3
period = 1
each = 0

update()

root.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79