title: Python - successive random calls to the same method
body: <p>On the following strip-down version of my program, I want to simulate an exchange where the user and the computer will act following a random sequence. </p>
  
  <p>Here the variable  <code>row</code> contains the sequence order. A value of 0 means the program is waiting for a user input (method <code>val0</code>). A value of 1 means there should be an automatic process (method <code>val1</code>).</p>
  
  <p>It seems to work at the beginning when alternating 0 and 1 but as soon as we are waiting for two successive automatic calls, it goes out of whack.</p>
  
  <p>I tried using the <code>after</code> method but I can't see how and where to insert it.</p>
  
  <p>A <code>while</code> loop might do the trick on this example but the end program is more complex, with sequence interruption, reevaluation of the sequence and so on. So I don't know if it would still apply then.</p>
  
  <pre><code>    from tkinter import *
  
  class Application:  
      def __init__(self,master = None):
          self.master = master
          Label(master,text='press next').grid()
          Button(master,text='Next',command=self.val0).grid()
          self.index = IntVar()
          self.index.set(0)
          self.index.trace("w",self.nextTurn)
  
      def val0(self):
          print("User action")
          self.index.set(self.index.get() +1)
  
      def val1(self):
          print("Automatic action")
          self.index.set(self.index.get() +1)
  
      def nextTurn(self, *args):
          i = self.index.get()
          if i &gt;= len(row):
              self.master.destroy()
              return
          if row[i] == 1:
              self.val1()
  
  
  if __name__ == "__main__":
      row = [0,1,0,0,1,1,0,0,0,1,1,1]
      root = Tk()
      win = Application(root)
      root.mainloop()
  </code></pre>