一、搭建GUI界面的方式
搭建GUI界面通常有两种方式:纯手码、手码+设计工具
二、QtDesigner的介绍
三、关于QtDesigner的使用与在PyCharm中的配置
四、QtDsigner的具体使用
1、界面认识
2、常用操作
3、文件使用
通过UI文件转换过来的py文件:
1 # -*- coding: utf-8 -*- 2 3 # Form implementation generated from reading ui file 'login.ui' 4 # 5 # Created by: PyQt5 UI code generator 5.14.2 6 # 7 # WARNING! All changes made in this file will be lost! 8 9 10 from PyQt5 import QtCore, QtGui, QtWidgets 11 12 13 class Ui_Form(object): 14 def setupUi(self, Form): 15 Form.setObjectName("Form") 16 Form.resize(749, 632) 17 self.pushButton = QtWidgets.QPushButton(Form) 18 self.pushButton.setGeometry(QtCore.QRect(120, 250, 111, 101)) 19 self.pushButton.setStyleSheet("background-color: rgb(0, 255, 255); " 20 "border-image: url(:/login_pic/xxx.png);") 21 self.pushButton.setAutoDefault(False) 22 self.pushButton.setObjectName("pushButton") 23 self.widget = QtWidgets.QWidget(Form) 24 self.widget.setGeometry(QtCore.QRect(120, 140, 326, 65)) 25 self.widget.setObjectName("widget") 26 self.verticalLayout = QtWidgets.QVBoxLayout(self.widget) 27 self.verticalLayout.setContentsMargins(0, 0, 0, 0) 28 self.verticalLayout.setObjectName("verticalLayout") 29 self.horizontalLayout = QtWidgets.QHBoxLayout() 30 self.horizontalLayout.setObjectName("horizontalLayout") 31 self.label = QtWidgets.QLabel(self.widget) 32 font = QtGui.QFont() 33 font.setPointSize(12) 34 self.label.setFont(font) 35 self.label.setObjectName("label") 36 self.horizontalLayout.addWidget(self.label) 37 self.lineEdit = QtWidgets.QLineEdit(self.widget) 38 self.lineEdit.setObjectName("lineEdit") 39 self.horizontalLayout.addWidget(self.lineEdit) 40 self.verticalLayout.addLayout(self.horizontalLayout) 41 self.horizontalLayout_2 = QtWidgets.QHBoxLayout() 42 self.horizontalLayout_2.setObjectName("horizontalLayout_2") 43 self.label_2 = QtWidgets.QLabel(self.widget) 44 font = QtGui.QFont() 45 font.setPointSize(12) 46 self.label_2.setFont(font) 47 self.label_2.setObjectName("label_2") 48 self.horizontalLayout_2.addWidget(self.label_2) 49 self.lineEdit_2 = QtWidgets.QLineEdit(self.widget) 50 self.lineEdit_2.setObjectName("lineEdit_2") 51 self.horizontalLayout_2.addWidget(self.lineEdit_2) 52 self.verticalLayout.addLayout(self.horizontalLayout_2) 53 self.label.setBuddy(self.lineEdit) 54 self.label_2.setBuddy(self.lineEdit_2) 55 56 self.retranslateUi(Form) 57 self.pushButton.clicked.connect(Form.login_test) 58 QtCore.QMetaObject.connectSlotsByName(Form) 59 60 def retranslateUi(self, Form): 61 _translate = QtCore.QCoreApplication.translate 62 Form.setWindowTitle(_translate("Form", "Form")) 63 self.pushButton.setText(_translate("Form", "登录")) 64 self.label.setText(_translate("Form", "用户(&n):")) 65 self.label_2.setText(_translate("Form", "密码(&p):")) 66 import login_button_pic_rc 67 68 69 if __name__ == "__main__": 70 import sys 71 app = QtWidgets.QApplication(sys.argv) 72 Form = QtWidgets.QWidget() 73 ui = Ui_Form() 74 ui.setupUi(Form) 75 Form.show() 76 sys.exit(app.exec_())
创建一个py文件来加载UI文件里的东西:
1 # *******************使用ui文件**********************开始 2 from PyQt5.Qt import * 3 from login import Ui_Form 4 5 class Window(QWidget,Ui_Form): # 多继承 6 def __init__(self): 7 super().__init__() 8 self.setWindowTitle("ui文件使用") 9 self.resize(500, 500) 10 self.setupUi(self) # Ui文件的参数导入 11 self.setup_ui() 12 13 def setup_ui(self): 14 pass 15 16 def login_test(self): 17 print(self.lineEdit.text()) 18 print(self.lineEdit_2.text()) 19 20 if __name__ == '__main__': 21 import sys 22 23 app=QApplication(sys.argv) 24 25 window=Window() 26 window.show() 27 sys.exit(app.exec_()) 28 # *******************使用ui文件**********************结束