Qtdesigner的安装大家可以参考我学长的博文:https://www.jianshu.com/p/8b992e47a0e4
然后就是我们要进行工作了,拖拽建页面就不说了,这个很简单。
我们需要保存.ui文件,假如文件名demo.ui,我们在控制台将文件转化成.py文件,然后在pycharm编辑,可以实现界面和业务逻辑的分离。
命令:
pyuic5 -o demo.py demo.ui
接下来是我们demo.py文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'demo.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_User_Form(object):
def setupUi(self, User_Form):
User_Form.setObjectName("User_Form")
User_Form.resize(846, 356)
self.OK_PushButton = QtWidgets.QPushButton(User_Form)
self.OK_PushButton.setGeometry(QtCore.QRect(180, 219, 93, 28))
self.OK_PushButton.setObjectName("OK_PushButton")
self.Cancle_pushButton = QtWidgets.QPushButton(User_Form)
self.Cancle_pushButton.setGeometry(QtCore.QRect(320, 219, 93, 28))
self.Cancle_pushButton.setObjectName("Cancle_pushButton")
self.User_label = QtWidgets.QLabel(User_Form)
self.User_label.setGeometry(QtCore.QRect(170, 109, 72, 15))
self.User_label.setObjectName("User_label")
self.Pwd_label = QtWidgets.QLabel(User_Form)
self.Pwd_label.setGeometry(QtCore.QRect(140, 159, 72, 15))
self.Pwd_label.setObjectName("Pwd_label")
self.User_lineEdit = QtWidgets.QLineEdit(User_Form)
self.User_lineEdit.setGeometry(QtCore.QRect(220, 99, 181, 31))
self.User_lineEdit.setObjectName("User_lineEdit")
self.Pwd_lineEdit = QtWidgets.QLineEdit(User_Form)
self.Pwd_lineEdit.setGeometry(QtCore.QRect(220, 149, 181, 31))
self.Pwd_lineEdit.setObjectName("Pwd_lineEdit")
self.User_textBrowser = QtWidgets.QTextBrowser(User_Form)
self.User_textBrowser.setGeometry(QtCore.QRect(450, 70, 256, 171))
self.User_textBrowser.setObjectName("User_textBrowser")
self.retranslateUi(User_Form)
QtCore.QMetaObject.connectSlotsByName(User_Form)
def retranslateUi(self, User_Form):
_translate = QtCore.QCoreApplication.translate
User_Form.setWindowTitle(_translate("User_Form", "Form"))
self.OK_PushButton.setText(_translate("User_Form", "OK"))
self.Cancle_pushButton.setText(_translate("User_Form", "Cancle"))
self.User_label.setText(_translate("User_Form", "User:"))
self.Pwd_label.setText(_translate("User_Form", "Password:"))
接下来重新建Call_demo文件对demo.py进行调用,实现界面和业务逻辑分离。
import sys
sys.path.append(r'E:\python_p\pyqt5test') #由于demo模块没有在sys的路径里,所以需要导入
from PyQt5.QtWidgets import QApplication, QMainWindow
from demo import Ui_User_Form
class MyMainForm(QMainWindow, Ui_User_Form):
def __init__(self, parent = None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
#固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
#初始化
myWin = MyMainForm()
#将窗口控件显示在屏幕上
myWin.show()
#程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())
运行结果:
添加信号和槽:
实现部分见代码注释。这里主要添加如下两行命令配置信号和槽的关系。信号和槽的创建和原理以后描述。这里可以参照添加即可。
self.OK_PushButton.clicked.connect(self.display)#给pushbutton添加display信号量
self.Cancle_pushButton.clicked.connect(self.close) #给pushbutton添加close信号量
代码如下:
import sys
sys.path.append(r'E:\python_p\pyqt5test') #由于demo模块没有在sys的路径里,所以需要导入
from PyQt5.QtWidgets import QApplication, QMainWindow
from demo import Ui_User_Form
class MyMainForm(QMainWindow, Ui_User_Form):
def __init__(self, parent = None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.OK_PushButton.clicked.connect(self.display)#给pushbutton添加display信号量
self.Cancle_pushButton.clicked.connect(self.close) #给pushbu添加close信号量
def display(self):
username = self.User_lineEdit.text() #读取lineEdit文本内容
password = self.Pwd_lineEdit.text()
self.User_textBrowser.setText("Username = " + username+ ";\n Password = " +password ) #在textBrowser框里输出文本
if __name__ == "__main__":
#固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
#初始化
myWin = MyMainForm()
#将窗口控件显示在屏幕上
myWin.show()
#程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())
运行结果:
到这里我们的第一个界面就算完成了,还有很多功能会在以后介绍。
很多函数的作用我都已经写在了注释里了。