-1

I'm trying to make a script with tkinter that displays some elements one after each other (not all of them at the same time) using .after().

The problem I have is that all elements are displayed at the same time after the overall waiting time has finished (overall waiting time = sum of the individual time of each element).

I've checked many examples on the web, and I guess I'm using the .after() method wrong, but I just can't figure out why.

I would expect the following code to update the label wait for 0.5 seconds after each loop. Nonetheless, what I get is a total wait of 3 seconds, after which the tkinter window appears, showing the last element of the list.

import tkinter as tk
import random

grid = [1, 2, 3, 4, 5, 6]

root = tk.Tk()

canvas = tk.Canvas(root, width=200, height=200)
canvas.pack()

def refresh():
    for i in range(len(grid)):
        text = grid[i]
        label.configure(text=text)

        root.after(500)

label = tk.Label(canvas, text='0', font='Courier 18')
label.place(relx=0.45, rely=0.4)

refresh()

root.mainloop()

Thanks in advance!

martifapa
  • 107
  • 3
  • 12
  • 1
    Does this answer your question? [tkinter: how to use after method](https://stackoverflow.com/a/25753719/7414759) – stovfl Jun 13 '20 at 12:53
  • Hey stovfl! I had already seen this post, but the thing is I wanted to display the elements in an orderly manner (not randomly)... Now I've been able to come to a solution, though I'm not sure it may be the most efficient... Thanks anyway!! – martifapa Jun 13 '20 at 13:18
  • ***orderly manner (not randomly).***: Behave the same, you have to use a generator or global index variable. – stovfl Jun 13 '20 at 13:20

1 Answers1

0

Yes, it's quite normal, but you have to add an handler after the time delay. If you don't, the computer only freezes the program until the time runs out.

Try this:

root.after(500, handler)

And what does it does is you create a function for the handler and after the 500ms the program will call that function.

10 Rep
  • 2,217
  • 7
  • 19
  • 33