title: Tkinter understanding mainloop
body: <p>Till now, I used to end my Tkinter programs with: <code>tk.mainloop()</code>, or nothing would show up! See example:</p>
  <pre><code>from Tkinter import *
  import random
  import time
  
  tk = Tk()
  tk.title = &quot;Game&quot;
  tk.resizable(0,0)
  tk.wm_attributes(&quot;-topmost&quot;, 1)
  
  canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
  canvas.pack()
  
  class Ball:
      def __init__(self, canvas, color):
          self.canvas = canvas
          self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
          self.canvas.move(self.id, 245, 100)
      def draw(self):
          pass
  
  ball = Ball(canvas, &quot;red&quot;)
  
  tk.mainloop()
  </code></pre>
  <p>However, when tried the next step in this program (making the ball move by time), the book am reading from, says to do the following. So I changed the draw function to:</p>
  <pre><code>def draw(self):
      self.canvas.move(self.id, 0, -1)
  </code></pre>
  <p>and add the following code to my program:</p>
  <pre><code>while 1:
      ball.draw()
      tk.update_idletasks()
      tk.update()
      time.sleep(0.01)
  </code></pre>
  <p>But I noticed that adding this block of code, made the use of <code>tk.mainloop()</code> useless, since everything would show up even without it!!!</p>
  <p><em>At this moment I should mention that my book never talks about <code>tk.mainloop()</code> (maybe because it uses Python 3) but I learned about it searching the web since my programs didn't work by copying book's code!</em></p>
  <p>So I tried doing the following that would not work!!!</p>
  <pre><code>while 1:
      ball.draw()
      tk.mainloop()
      time.sleep(0.01)
  </code></pre>
  <p>What's going on? What does <code>tk.mainloop()</code>? What does <code>tk.update_idletasks()</code> and <code>tk.update()</code> do and how that differs from <code>tk.mainloop()</code>? Should I use the above loop?<code>tk.mainloop()</code>? or both in my programs?</p>