title: How do I wait for a certain number of buttons to be clicked in Tkinter/Python?
body: <p>I'm trying to write a simple 'Simon' game, but I have hit a road block here, and honestly have no idea how to get around it. </p>
  
  <p>So here, I made a class for the four buttons in the GUI:</p>
  
  <pre><code>class button:
      def buttonclicked(self):
              self.butclicked= True
  
      def checkIfClicked(self):
              if self.butclicked== True:
                      global pressed
                      pressed.append(self.color)
                      self.butclicked= False
  
      def __init__(self, color1):
              self.color= color1
          self.button= tk.Button(root, text=' '*10, bg= self.color, command= self.buttonclicked)
          self.button.pack(side='left')
          self.butclicked=False
  </code></pre>
  
  <p>I then created four instances of this class in <code>blue, red, yellow, and green as bb, rb, yb,</code> and <code>gb</code>.</p>
  
  <p>Once everything is packed into the Tk() module, it enters a while loop that appends a random color to a list activecolors. I try to use the following loop to wait until the list pressed is at least as long as the list activecolors before comparing the two to see if the user was correct:</p>
  
  <pre><code>while len(pressed)&lt;len(activecolors):
                      sleep(.25)
                      print('In the check loop')
                      bb.checkIfClicked()
                      rb.checkIfClicked()
                      yb.checkIfClicked()
                      gb.checkIfClicked()
  </code></pre>
  
  <p>However, since it is stuck inside the while loop, the program can't tell that the button has been clicked. I thought adding the sleep method into the loop would allow the code to have time to do other things (such as process button clicks), but this is not the case. Any help is appreciated.</p>
  
  <p><a href="https://docs.google.com/document/d/16zQlQk_mFoSro7iq_8NDYWen2QwfhGNUgcs7JSkFwvU/edit?usp=sharing" rel="nofollow">Here is the link</a> to the full code, if you would like to see it. A warning though: it's not pretty.</p>
  
  <p>Edit:
  I ended up just changing the code to check the list only after a new button was clicked, telling the computer the code was ready. I've updated the Google Document if you'd like to see it.</p>