0

I'm using Tkinter to make a pi-memorization game. (Python 2.7) My main loop has to both update the Tkinter window, and display new digits of pi for you to copy. My first code for the loop was

wait = 5
for digit in pilist:
    if wait > 1:
        wait -= 1
    display.delete("all")
    display.create_text((50, 50), text=digit)
    tk.update()
    time.sleep(wait)

This worked fine, the speed increased, and the numbers advanced. But typing into the entry widget appeared sluggish, because it would only update every loop, and the loop was slowed down. Then I tried this:

wait = 5
initialtime = time.time()
for digit in pilist:
    if wait > 1:
        wait -= 1
    newtime = time.time()
    if newtime - initialtime >= wait:
        initialtime = time.time()
        display.delete("all")
        display.create_text((50, 50), text=digit)
    tk.update()

The list would update fine, but my list (with 333 elements in it,) would skip elements, because the list would advance, even though the only numbers being displayed were the ones that happened to fall every wait seconds when the if statement became true. So, how do I advance the list every wait seconds, but still update faster? I hope my question is clear.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Luke Taylor
  • 8,631
  • 8
  • 54
  • 92
  • Don't use `time` with tkinter. Use instead the `after` method... – nbro Feb 02 '15 at 19:39
  • I mean, I understand what it does, but as far as I can see, it can only be used to schedule the calling of a defined function... how would I adapt this to use a function? I don't think after can pass arguments to a function... Anyway, I'm going sledding now, so I'll see replies in about an hour. It snowed 7 inches :) – Luke Taylor Feb 02 '15 at 19:53
  • Can anyone answer this? – Luke Taylor Feb 02 '15 at 21:12
  • here's code example that shows how to [use `.after()` method to run a function periodically](http://stackoverflow.com/a/14040516/4279) – jfs Feb 03 '15 at 11:22
  • Thanks a lot. Wanna post it as an answer so that I can mark it as correct? – Luke Taylor Feb 05 '15 at 01:42

0 Answers0