0
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(60, 160, 201, 81))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(60, 110, 201, 41))
        self.lineEdit.setObjectName("lineEdit")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(70, 50, 191, 41))
        self.label.setText("")
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Push ME!!"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Sorry if this is a very basic question, but I've tried to search from Google to Youtube without result. So, please help me.

I want to make so that when I click the button with the text inside lineedit is "OK", the result will show up in label with this sentence: "You're Good!"

Thank you!

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
IvanR8
  • 1

1 Answers1

0

Read this bit first:

This is a trivial example - but I'd strongly recommend you completely decouple any logic onto separate .py files from your GUI. That allows changes to with the GUI editor without losing your logic.

Anyway, I've stuck with your architecture such as it is for this answer - but I'd really advise you steer away from it ASAP.

.ui - .py should be their own file(s)

pythonInterfaceToGUI.py should be their own file(s)

pythonLogic.py - should be their own file(s)

pythonInterfaceToGUI defines the hooks to the GUI and the calls to the logic.


You need to connect the button to something, did you come across anything like:

self.pushbutton.clicked.connect(self.foo)

and here foo would be

def foo(self):

    self.lineEdit.setText("Your good!")

Put together, here your .py would be:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton =     QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(60, 160, 201, 81))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(60, 110, 201, 41))
        self.lineEdit.setObjectName("lineEdit")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(70, 50, 191, 41))
        self.label.setText("")
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        self.pushButton.clicked.connect(self.foo)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Push ME!!"))


    def foo(self):
        self.lineEdit.setText("Your good!")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Amiga500
  • 1,258
  • 1
  • 6
  • 11
  • Since you also agree that ui and logic should be decoupled, why don't you actually apply that concept in the answer? That's just a matter of `import`, and doesn't lead the OP to believe that doing the above is anyway an accepted practice as people often does (and in PyQt it's considered a particularly despicable one indeed). – musicamante Mar 08 '22 at 13:37
  • 'cos I don't believe in throwing too much at someone new to the topic at once. They'll find their feet, then go from there. – Amiga500 Mar 08 '22 at 14:45
  • How is adding an import and explaining a basic concept "throwing too much"? Do you realize that we get at least 10 posts a week from people that have always the *same* issues because they try to manually edit pyuic files, without understanding the consequences of doing it? – musicamante Mar 08 '22 at 14:56
  • Yep. When connecting a button is still on the learning curve, reshaping the whole solution is too much. Baby steps. The point was made, they can then act on it down the line when they understand the fundamentals. Rather than present separate .pys, introduce adding to the system path and then importing the pys. You don't realise how much you already know and take for granted. – Amiga500 Mar 08 '22 at 15:09
  • I'm very aware of how knowledge and the cost of acquiring it is perceived differently throughout experience. Bering a teacher I also know that with beginners we must provide "collateral" concepts that won't be immediately explained, will be taken for granted and eventually understood at a later point for the sake of proper learning: the OP doesn't need to understand inheritance, but *must* know that the above is bad practice and shouldn't be done. Suggesting a bad practice because "it's easier" is not a valid excuse, especially since we can't follow the learning of those we give suggestions to – musicamante Mar 08 '22 at 15:57
  • Well, if you feel so strongly about it - go ahead and stick up the properly architected example. That'll then give them the small step to understand where they are wrong right now and then where they should aim to get to. – Amiga500 Mar 08 '22 at 16:47
  • 1
    @Amiga500 The problem with your "solution" is that it will be lost when the ui module is regenerated. You don't give sufficient emphasis to this very common issue in your answer, so you're really not doing the OP any favours by attempting to side-step it. The correct solution is simple enough to explain and illustrate with small demo script - why not just the give the OP the benefit of the doubt and assume they'll be able to understand it? Just because they're unfamiliar with pyqt, does not mean they know nothing about python (or about programming in general). – ekhumoro Mar 08 '22 at 17:05