PyQt QPushButton signal handling
<p>I have a <code>repeat</code> python function and a <em>test.ui</em> which has only one push-button. My doubt is how to loop the same function exactly once for every time the button is clicked. Because for me whenever I perform:</p>

<pre><code>self.pushButton.clicked.connect(self.repeat)
</code></pre>

<p>it loops many times into the function instead of a single time. I found this by incrementing a value for every time we reach the function. How to reach the function <code>repeat</code> exactly once for every click on the push-button?</p>

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

qtCreatorFile = "test.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class Login(QtGui.QMainWindow, Ui_MainWindow):
    i=1
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)

        self.setupUi(self)
        self.pushButton.setText("iam in init")
        self.pushButton.clicked.connect(self.repeat)
    def repeat(self):

        self.pushButton.setText("iam in repeat"+str(self.i))

        self.i=self.i+1

        self.pushButton.clicked.connect(self.repeat)




if __name__ == "__main__":
    app=QtGui.QApplication(sys.argv)
    main = Login()
    main.show()
    sys.exit(app.exec_())
</code></pre>

<p>Loooking at your code, you have established the connection multiple times. You should establish it with <code>self.pushButton.clicked.connect(self.repeat)</code> only in your <code>__init__</code> but not in <code>repeat()</code> function. In other words, delete the second occurrence (i.e. in <code>repeat()</code>) and you should be fine. The connection should be established only once because once established it lasts till <code>disconnect()</code> is called on it or till the <code>slot</code> or the <code>signal</code> are destroyed.</p>

python pyqt4 signals-slots qpushbutton