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!

0

1 Answer 1

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_())
Sign up to request clarification or add additional context in comments.

7 Comments

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).
'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.
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?
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 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).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.