0

I have a small pyqt5 code, where I am expecting to enter a name, and print it out when I click a button. However, what it is actually doing is that the button is not working, and the string is printed when I press after the string entered. This is on a mac.

How can I print the string only when the button is clicked?

The calling code is the following:

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from demoLineEdit import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.ButtonClickMe.clicked.connect(self.dispmessage)
        self.show()

    def dispmessage(self):
        self.ui.labelResponse.setText("Hello "
                   +self.ui.lineEditName.text())

if __name__=="__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

The manually written UI code is the following:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(379, 195)
        self.label1 = QtWidgets.QLabel(Dialog)
        self.label1.setGeometry(QtCore.QRect(6, 40, 121, 20))
        self.label1.setObjectName("label1")
        self.labelResponse = QtWidgets.QLabel(Dialog)
        self.labelResponse.setGeometry(QtCore.QRect(40, 90, 271, 20))
        self.labelResponse.setObjectName("labelResponse")
        self.lineEditName = QtWidgets.QLineEdit(Dialog)
        self.lineEditName.setGeometry(QtCore.QRect(140, 40, 201, 20))
        self.lineEditName.setObjectName("lineEditName")
        self.ButtonClickMe = QtWidgets.QPushButton(Dialog)
        self.ButtonClickMe.setGeometry(QtCore.QRect(160, 130, 101, 23))
        self.ButtonClickMe.setObjectName("ButtonClickMe")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label1.setText(_translate("Dialog", "Enter your name"))
        self.labelResponse.setText("")
        self.ButtonClickMe.setText(_translate("Dialog", "Click"))

I also tried generating the UI code from Pyside, but the same behavior is seen. This is the generated code:

from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(401, 290)
        self.label1 = QLabel(Dialog)
        self.label1.setObjectName(u"label1")
        self.label1.setGeometry(QRect(30, 200, 71, 16))
        self.labelResponse = QLabel(Dialog)
        self.labelResponse.setObjectName(u"labelResponse")
        self.labelResponse.setGeometry(QRect(30, 230, 60, 16))
        self.lineEditName = QLineEdit(Dialog)
        self.lineEditName.setObjectName(u"lineEditName")
        self.lineEditName.setGeometry(QRect(110, 200, 113, 21))
        self.ButtonClickMe = QPushButton(Dialog)
        self.ButtonClickMe.setObjectName(u"ButtonClickMe")
        self.ButtonClickMe.setGeometry(QRect(240, 210, 113, 32))

        self.retranslateUi(Dialog)

        QMetaObject.connectSlotsByName(Dialog)
    # setupUi

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
        self.label1.setText(QCoreApplication.translate("Dialog", u"Enter name:", None))
        self.labelResponse.setText("")
        self.ButtonClickMe.setText(QCoreApplication.translate("Dialog", u"Click", None))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
R71
  • 4,283
  • 7
  • 32
  • 60
  • Could you explain me better, try using `self.ui.labelResponse.adjustSize()` `self.ui.labelResponse.repaint()` after `self.ui.labelResponse.setText("Hello "+self.ui.lineEditName.text())` – eyllanesc Feb 14 '20 at 08:54
  • Thanks, adding the repaint line solved the problem. If you post this as an answer, I will accept it. – R71 Feb 14 '20 at 09:38
  • But another question - I am calling dispmessage on ButtonClickMe, so why is the string getting printed when I type after the string? – R71 Feb 14 '20 at 09:40
  • see https://stackoverflow.com/questions/44005056/how-to-make-qpushbutton-not-to-be-triggered-by-enter-keyboard-key – eyllanesc Feb 14 '20 at 09:45
  • Thanks, that solved the problem – R71 Feb 15 '20 at 04:54

0 Answers0