2

I have a repeat python function and a test.ui 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:

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

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 repeat exactly once for every click on the push-button?

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_())

1 Answers1

2

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