title: turtle graphics: How do I implement a pause function?
body: <p>I'm trying to use python 3 turtle graphics to do something like presentation software: draw something, pause for a keystroke so the presenter can explain, then draw the next thing.</p>
  
  <p>Here is one solution I've tried (that doesn't work):</p>
  
  <pre><code>import turtle
  import time
  
  paused = False
  
  def unpause():
      print("unpause() called")
      global paused
      paused = False
  
  def pause():
      global paused
      paused = True
      while paused:
          time.sleep(0.1)
  
  
  t = turtle.Turtle()
  
  # set up listener
  t.screen.listen()
  t.screen.onkeypress(unpause)
  
  # draw something
  t.hideturtle()
  t.pensize(5)
  t.speed(1)
  t.pu()
  t.goto(-300,-300)
  t.pd()
  t.goto(-300, 300)
  
  # pause until key is pressed
  pause()
  
  # draw some more
  t.pu()
  t.goto(300,-300)
  t.pd()
  t.goto(300, 300)
  
  t.screen.mainloop()
  </code></pre>
  
  <p>The problem is that the sleep call loop totally blocks the keypress from being detected, even when I use a while loop of very short (100ms) sleeps.</p>
  
  <p>If I hit a key while the first line is drawing, I see "unpause() called" in my console, so I know that the key binding is active.</p>
  
  <p>Why doesn't the keypress get detected? I don't know about the internals, but I thought that the keystroke would be recorded in a buffer somewhere, and during the break between sleep calls, the listener would read the buffer and unset the <code>paused</code> global variable. This is not happening.</p>
  
  <p>Is there some other way I could implement this?</p>
  
  <p>This is on a Debian Linux system.</p>