In this Lesson we are going to learn creating of PyQt5 QLabel & QLineEdit with Qt Designer, so in this tutorial we want to use Qt Designer, first of all you need to open your Qt Designer. you can write pyqt5designer in your terminal, after opening qt designer, now we want to design our simple application. basically we need a QLabel, QLineEdit with QPushButton, we want to add these widgets in the Qt Designer. also we want to add horizontal layout for the widgets.
You need to select the first two widgets and make the Layout Horizontally, after that repeat this for the second two widgets.
Now you need to right click on the window and make the window vertically layout.
After that save your ui design and copy the ui file in the script folder of your python installation, now let’s convert our ui file in to python file. make sure that you have copied your file in to Scripts folder. because we are using pyuic5 module for converting our ui file in to python. now open your terminal and write this command.
1 |
pyuic5 label.ui -o mylabelexample.py -x |
Your ui file is converted to python file and this is the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# -*- coding: utf-8 -*- # Form implementation generated from # reading ui file 'label.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # WARNING: Any manual changes made to this #file will be lost when pyuic5 is # run again. Do not edit this file unless you # know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import QFont class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(376, 155) self.verticalLayout = QtWidgets.QVBoxLayout(Form) self.verticalLayout.setObjectName("verticalLayout") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.labelName = QtWidgets.QLabel(Form) self.labelName.setObjectName("labelName") self.horizontalLayout.addWidget(self.labelName) self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setObjectName("lineEdit") self.horizontalLayout.addWidget(self.lineEdit) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.labelResult = QtWidgets.QLabel(Form) self.labelResult.setObjectName("labelResult") self.labelResult.setFont(QFont("Sanserif", 15)) self.labelResult.setStyleSheet('background-color:yellow') self.labelResult.setStyleSheet('color:red') self.horizontalLayout_2.addWidget(self.labelResult) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setObjectName("pushButton") self.pushButton.clicked.connect(self.click_btn) self.horizontalLayout_2.addWidget(self.pushButton) self.verticalLayout.addLayout(self.horizontalLayout_2) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "PyQt5 QLabel & QLineEdit")) self.labelName.setText(_translate("Form", "Name:")) self.labelResult.setText(_translate("Form", "TextLabel")) self.pushButton.setText(_translate("Form", "Click")) def click_btn(self): name = self.lineEdit.text() self.labelResult.setText(name) #print(name) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_()) |
Just in here we have added a new python code of click_btn() because we have connected this method with the clicked signal of our QPushButton, i want when a user write his name and click on the button, the label text should be changed.
1 2 3 |
def click_btn(self): name = self.lineEdit.text() self.labelResult.setText(name) |
And this is the clicked signal of the QPushButton.
1 |
self.pushButton.clicked.connect(self.click_btn) |
Run the complete code and this is the result.