Blocking button click signals in PyQt
<p>I have a program that uses pyqt's .animateClick() feature to show the user a sequence of different button clicks that the user has to copy in that specific order. The problem is I don't want the animateClick() to send a signal, I only want the button click signals from the user. Here is some of my code to demonstrate what I mean, and how I tried to solve that problem (that doesn't work). I simplified my code quite a bit so its easier to read, let me know if you have any questions.</p>

<pre><code>from PyQt4 import QtCore,QtGui
global flag
global ai_states
ai_states = []
user_states = []


class Program(object):
    # Set up the push buttons
    #Code Here.

    # Connect push buttons to function get_state()
    self.pushButton.clicked.connect(self.get_state)
    self.pushButton_2.clicked.connect(self.get_state)
    self.pushButton_3.clicked.connect(self.get_state)        
    self.pushButton_4.clicked.connect(self.get_state)

    # Code that starts the start() function

    def start(self):
        flag = 0
        ai_states[:] = []
        i = -1
        # Code here that generates ai_states, numbers 1-4, in any order, based on button numbers.

        for k in ai_states:
            i = i + 1
            # Code here that animates button clicks determined by ai_states

            # Changes the flag to 1 once the loop ends
            if i == len(ai_states):
                flag = 1

    def get_state(self):
        if flag == 1:
            user_states.append(str(self.centralWidget.sender().text()))
        else:
            pass

        if len(user_states) == len(ai_states):
            # Checks to make sure the user inputted the same clicks as the ai_states
</code></pre>

<p>Even though the flag does come out to be 1 after the start() function, it is still appending the animatedClick() signals. What am I doing wrong? I'm new to GUI programming, so I'm probably going about this in a very bad way. Any help would be appreciated.</p>

<p>Never use global variables unless you really have to. If you need shared access to variables, use instance attributes:</p>

<pre><code>from PyQt4 import QtCore,QtGui   

class Program(object):
    def __init__(self):
        self.ai_states = []
        self.user_states = []
        self.flag = 1

        # Set up the push buttons
        # Code Here

        # Connect push buttons to function get_state()
        self.pushButton.clicked.connect(self.get_state)
        self.pushButton_2.clicked.connect(self.get_state)
        self.pushButton_3.clicked.connect(self.get_state)        
        self.pushButton_4.clicked.connect(self.get_state)

    # Code that starts the start() function

    def start(self):
        self.flag = 0
        del self.ai_states[:]
        i = -1
        # Code here that generates ai_states, numbers 1-4, in any order, based on button numbers.

        for k in self.ai_states:
            i = i + 1
            # Code here that animates button clicks determined by ai_states

        # Changes the flag to 1 once the loop ends
        self.flag = 1

    def get_state(self):
        if self.flag == 1:
            self.user_states.append(str(self.centralWidget.sender().text()))

        if len(self.user_states) == len(self.ai_states):
            # Checks to make sure the user inputted the same clicks as the ai_states
</code></pre>

python user-interface pyqt signals