title: Tkinter — executing functions over time
body: <p>I'm trying to figure out how the tkinter control flow works.</p>
  
  <p>I want to display a rectangle and to make it blink three times. I wrote this code, but it doesn't work. I guess it's because <code>blink</code> is executed before <code>mainloop</code>, and it doesn't actually draw anything. If so, how can I swap the control flow between <code>blink</code> and <code>mainloop</code> to make it work?</p>
  
  <p>My code:</p>
  
  <pre><code>from tkinter import *
  from time import *
  
  def blink(rectangle, canvas):
      for i in range(3):
          canvas.itemconfigure(rectangle, fill = "red")
          sleep(1)
          canvas.itemconfigure(rectangle, fill = "white")
          sleep(1)
  
  root = Tk()
  fr = Frame(root)
  fr.pack()
  canv = Canvas(fr, height = 100, width = 100)
  canv.pack()
  rect = canv.create_rectangle(25, 25, 75, 75, fill = "white")
  blink(rect, canv)
  root.mainloop()
  </code></pre>