harthart
harthart

Reputation: 414

How to Make a Button Perform Single Click Instead of Double Click

I'm experimenting on PyQt5. When I enter a value and click the push button to pass the value entered on my line edit, I noticed that it requires 2 mouse clicks for it to pass, and display the value on my plain text edit.

How will I change my codes to allow the entered value on the line edit to be passed by just 1 click?

from PyQt5 import QtCore, QtGui, QtWidgets
from functools import partial

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(640, 480)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(40, 80, 151, 31))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(190, 90, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(40, 170, 181, 16))
        self.label_2.setObjectName("label_2")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setGeometry(QtCore.QRect(220, 170, 104, 71))
        self.plainTextEdit.setReadOnly(True)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(310, 90, 75, 23))
        self.pushButton.setObjectName("pushButton")


        self.pushButton.clicked.connect(self.Pass)


        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Pass the value entered here:"))
        self.label_2.setText(_translate("Form", "Accept and display the passed value:"))
        self.pushButton.setText(_translate("Form", "PASS"))



    def Pass(self):

        accept = self.lineEdit.text()

        self.pushButton.clicked.connect(partial(self.Get, accept))

    def Get(self, getA):

        self.plainTextEdit.setPlainText(getA)




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()

    ui.Get(ui.Pass())

    sys.exit(app.exec_())

Upvotes: 2

Views: 505

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477607

.clicked fires the first time you click the button. The problem is however that you register Pass to the button:

self.pushButton.clicked.connect(self.Pass)

Now Pass is defined as:

def Pass(self):
    accept = self.lineEdit.text()
    # connect a new event
    self.pushButton.clicked.connect(partial(self.Get, accept))

So the first time you click the button, you read the accept, and only connect the self.Get method to the button such that the next time you hit the button, self.Get will indeed be called.

In order to process the request straight away, you can simply modify the Pass method to:

def Pass(self):
    accept = self.lineEdit.text()
    self.Get(accept)

Upvotes: 3

Related Questions