1.配置
Python3 PyQt5 pycharm 环境搭建见配置教程
安装完PyQt5之后,并没有Qt Designer.exe,还需进一步的安装。其安装方法是,在cmd中输入如下图所示的命令,pip install PyQt5-tools -i https://pypi.douban.com/simple --trusted-host=pypi.douban.com。这个命令下载的速度非常快,也可以使用通用的下载命令,pip install PyQt5-tools,这个命令就比较好记忆了,但是很慢。
2.原理
类似于matlab gui里的callback回调函数,pyqt5 有信号槽机制:
信号是事件发生,槽是对应的回调函数
信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重要地方。它为高层次的事件处理自动生成所需要的附加代码。在我们所熟知的很多 GUI 工具包中,窗口小部件(widget)都有一个回调函数用于响应它们能触发的每个动作,这个回调函数通常是一个指向某个函数的指针。但是,在QT中信号和槽取代了这些凌乱的函数指针,使得我们编写这些通信程序更为简洁明了
3.界面与逻辑代码分离
利用Qt designer我们可以做到界面与逻辑分离
1.首先利用Qt designer制作好界面
2.将界面利用Pyuic生成源码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'LSTM.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(618, 446)
Form.setAutoFillBackground(False)
Form.setInputMethodHints(QtCore.Qt.ImhNone)
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(0, 10, 241, 41))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.train = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.train.setObjectName("train")
self.horizontalLayout_2.addWidget(self.train)
self.apply = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.apply.setObjectName("apply")
self.horizontalLayout_2.addWidget(self.apply)
self.exit = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.exit.setObjectName("exit")
self.horizontalLayout_2.addWidget(self.exit)
self.frame_train = QtWidgets.QFrame(Form)
self.frame_train.setGeometry(QtCore.QRect(0, 60, 601, 381))
self.frame_train.setLocale(QtCore.QLocale(QtCore.QLocale.Chinese, QtCore.QLocale.China))
self.frame_train.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_train.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_train.setObjectName("frame_train")
self.groupBox = QtWidgets.QGroupBox(self.frame_train)
self.groupBox.setGeometry(QtCore.QRect(10, 20, 201, 211))
self.groupBox.setObjectName("groupBox")
self.epochs = QtWidgets.QLabel(self.groupBox)
self.epochs.setGeometry(QtCore.QRect(0, 30, 54, 12))
self.epochs.setObjectName("epochs")
self.epochsEdit = QtWidgets.QLineEdit(self.groupBox)
self.epochsEdit.setGeometry(QtCore.QRect(70, 20, 113, 20))
self.epochsEdit.setObjectName("epochsEdit")
self.batch_size = QtWidgets.QLabel(self.groupBox)
self.batch_size.setGeometry(QtCore.QRect(0, 60, 61, 16))
self.batch_size.setObjectName("batch_size")
self.batch_sizeEdit = QtWidgets.QLineEdit(self.groupBox)
self.batch_sizeEdit.setGeometry(QtCore.QRect(70, 60, 113, 20))
self.batch_sizeEdit.setObjectName("batch_sizeEdit")
self.input_size = QtWidgets.QLabel(self.groupBox)
self.input_size.setGeometry(QtCore.QRect(0, 100, 61, 16))
self.input_size.setLayoutDirection(QtCore.Qt.LeftToRight)
self.input_size.setObjectName("input_size")
self.input_sizeEdit = QtWidgets.QLineEdit(self.groupBox)
self.input_sizeEdit.setGeometry(QtCore.QRect(70, 100, 113, 20))
self.input_sizeEdit.setObjectName("input_sizeEdit")
self.time_step = QtWidgets.QLabel(self.groupBox)
self.time_step.setGeometry(QtCore.QRect(0, 140, 54, 12))
self.time_step.setObjectName("time_step")
self.time_stepEdit = QtWidgets.QLineEdit(self.groupBox)
self.time_stepEdit.setGeometry(QtCore.QRect(70, 140, 113, 20))
self.time_stepEdit.setObjectName("time_stepEdit")
self.verticalLayoutWidget = QtWidgets.QWidget(self.frame_train)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(230, 10, 291, 281))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.graphicsView_1 = QtWidgets.QGraphicsView(self.verticalLayoutWidget)
self.graphicsView_1.setObjectName("graphicsView_1")
self.verticalLayout.addWidget(self.graphicsView_1)
self.graphicsView_2 = QtWidgets.QGraphicsView(self.verticalLayoutWidget)
self.graphicsView_2.setObjectName("graphicsView_2")
self.verticalLayout.addWidget(self.graphicsView_2)
self.groupBox_2 = QtWidgets.QGroupBox(self.frame_train)
self.groupBox_2.setGeometry(QtCore.QRect(240, 300, 261, 80))
self.groupBox_2.setObjectName("groupBox_2")
self.Mae = QtWidgets.QLabel(self.groupBox_2)
self.Mae.setGeometry(QtCore.QRect(30, 30, 54, 12))
self.Mae.setObjectName("Mae")
self.maeEdit = QtWidgets.QLineEdit(self.groupBox_2)
self.maeEdit.setGeometry(QtCore.QRect(100, 20, 113, 20))
self.maeEdit.setObjectName("maeEdit")
self.Mape = QtWidgets.QLabel(self.groupBox_2)
self.Mape.setGeometry(QtCore.QRect(30, 50, 54, 12))
self.Mape.setObjectName("Mape")
self.mapeEdit = QtWidgets.QLineEdit(self.groupBox_2)
self.mapeEdit.setGeometry(QtCore.QRect(100, 50, 113, 20))
self.mapeEdit.setObjectName("mapeEdit")
self.import_data = QtWidgets.QPushButton(self.frame_train)
self.import_data.setGeometry(QtCore.QRect(90, 250, 75, 23))
self.import_data.setMouseTracking(False)
self.import_data.setLayoutDirection(QtCore.Qt.RightToLeft)
self.import_data.setObjectName("import_data")
self.start_train = QtWidgets.QPushButton(self.frame_train)
self.start_train.setGeometry(QtCore.QRect(90, 290, 75, 23))
self.start_train.setMouseTracking(False)
self.start_train.setLayoutDirection(QtCore.Qt.RightToLeft)
self.start_train.setObjectName("start_train")
self.retranslateUi(Form)
self.exit.clicked.connect(Form.close)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "LSTM Speed_predicter"))
Form.setWindowIcon(QIcon('suda.jpg'))
self.train.setText(_translate("Form", "train"))
self.apply.setText(_translate("Form", "apply"))
self.exit.setText(_translate("Form", "exit"))
self.groupBox.setTitle(_translate("Form", "Parameters"))
self.epochs.setText(_translate("Form", "epochs"))
self.batch_size.setText(_translate("Form", "batch_size"))
self.input_size.setText(_translate("Form", "input_size"))
self.time_step.setText(_translate("Form", "time_step"))
self.groupBox_2.setTitle(_translate("Form", "Evaluation"))
self.Mae.setText(_translate("Form", "Mae"))
self.Mape.setText(_translate("Form", "Mape"))
self.import_data.setText(_translate("Form", "import data"))
self.start_train.setText(_translate("Form", "start train"))
直接运行这个代码界面是不会出现的,还需要下面的代码:
if __name__=="__main__":
import sys
app=QtWidgets.QApplication(sys.argv)
widget=QtWidgets.QWidget()
ui=Ui_Form()
ui.setupUi(widget)#重载已经设计好的类,即界面
widget.show()
sys.exit(app.exec_()
3. 新建一个py文件,import刚才的文件,让界面与逻辑代码分离
一、直接继承界面类
import sys
import LSTM1
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MainCode(QWidget,LSTM1.Ui_Form):
def __init__(self):
QWidget.__init__(self)
LSTM1.Ui_Form.__init__(self)
self.setupUi(self)
#self.frame_train.setVisible(False)
#self.frame_apply.setVisible(False)
#self.train_state=0
#self.train.clicked.connect(self.show_train)
#self.apply.clicked.connect(self.show_apply)
#self.import_data.clicked.connect(self.showDialog)
#self.import_data1.clicked.connect(self.showDialog)
#self.start_train.clicked.connect(self.trainthread)
#self.evaluate.clicked.connect(self.evaluation)
#self.start_apply.clicked.connect(self.startapply)
if __name__=='__main__':
app=QApplication(sys.argv)
md=MainCode()
md.show()
sys.exit(app.exec_())
上面的带#号的语句即为相应的槽函数,可以自定义,通过这个来实现想要的逻辑
二、
from PyQt5.QtWidgets import *
from LSTM1 import Ui_Form
class MainCode(QWidget):
def __init__(self):
super(MainCode,self).__init__()
self.new=Ui_Form()
self.new.setupUi(self)
学习链接:
PyQt5学习笔记03----Qt Designer生成源码
Pycahrm+Qtdesign 快速开发GUI程序学习资源
基于python3.6+pyQT5利用Graphics View 控件显示图像并实现其缩放
pyqt5界面切换与之前matlab的原理类似,matlab是通过面板panel的visible属性,pyqt5则是通过frame的visible属性
-------组件设计--------
Python图形化界面编程 PyQt5中嵌入matplotlib