安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5==5.15.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5-tools==5.15.0.1.7
这里最好指定一下相同版本,如果使用默认版本可能会导致安装了不同的版本使用的时候可能会有一些奇怪的错误
生成ui文件
使用evetyThing搜索designer.exe, 使用 Qt/bin 中的designer.exe程序,通过拖拽生成*.ui文件, 使用 python路径/Tools/scripts/pyuic5.exe 生成 ui文件的py形式 命令:
pyuic5.exe main_window.ui -o main_window.py
然后拷贝main_window.py文件到项目中。
运行
有了main_window.py不行还是需要QMainWindow,我们可以使用如下代码,把我们生成的ui文件绑定到QMainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
#绑定ui文件到QMainWindow
self.setupUi(self)
# 绑定按钮的点击信号
self.text_check_btn.clicked.connect(self.text_check)
self.face_check_btn.clicked.connect(self.face_check)
self.modify_check_btn.clicked.connect(self.modify_check)
if __name__ == '__main__':
#这里开启了主循环
app = QApplication(sys.argv)
main_Window = MainWindow()
main_Window.show()
sys.exit(app.exec_())
自定义信号槽
自定义信号槽需要导入这个包 from PyQt5.QtCore import pyqtSignal
class TextCheckThread(QThread):
customSignal = pyqtSignal(str)
def __init__(self, t, parent=None):
super(TextCheckThread, self).__init__(parent)
self.t = t
def run(self):
for i in range(self.t):
time.sleep(1)
self.customSignal.emit(str(i))
这里的线程对象定义了一个自定义的信号槽,参数是QString类型,在线程启动时,每秒发射一次这个信号。
def fun_print(self, sss):
print("来自线程参数 ", sss)
self.thread.customSignal.connect(self.fun_print)
这里我们通过信号槽的connect()方法绑定了信号槽要执行的的函数,可以参考一下前面按钮的点击事件的处理。
附录一个demo源代码:
import sys
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QThread, pyqtSignal
class TextCheckThread(QThread):
customSignal = pyqtSignal(str)
def __init__(self, t, parent=None):
super(TextCheckThread, self).__init__(parent)
self.t = t
def run(self):
for i in range(self.t):
time.sleep(1)
self.customSignal.emit(str(i))
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.text_check_btn = QtWidgets.QPushButton(self.centralwidget)
self.text_check_btn.setGeometry(QtCore.QRect(290, 130, 141, 71))
self.text_check_btn.setStyleSheet("background-color: #FF9999;\n"
"border-radius:20px ")
self.text_check_btn.setObjectName("text_check_btn")
self.face_check_btn = QtWidgets.QPushButton(self.centralwidget)
self.face_check_btn.setGeometry(QtCore.QRect(290, 250, 141, 71))
self.face_check_btn.setStyleSheet("background-color: #FF9999;\n"
"border-radius:20px ")
self.face_check_btn.setObjectName("face_check_btn")
self.modify_check_btn = QtWidgets.QPushButton(self.centralwidget)
self.modify_check_btn.setGeometry(QtCore.QRect(290, 370, 141, 71))
self.modify_check_btn.setStyleSheet("background-color: #FF9999;\n"
"border-radius:20px ")
self.modify_check_btn.setObjectName("modify_check_btn")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(50, 30, 671, 61))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.layoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.file_path_et = QtWidgets.QLineEdit(self.layoutWidget)
self.file_path_et.setObjectName("file_path_et")
self.horizontalLayout.addWidget(self.file_path_et)
self.layoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget_2.setGeometry(QtCore.QRect(460, 260, 261, 51))
self.layoutWidget_2.setObjectName("layoutWidget_2")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.layoutWidget_2)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_3 = QtWidgets.QLabel(self.layoutWidget_2)
self.label_3.setObjectName("label_3")
self.horizontalLayout_3.addWidget(self.label_3)
self.face_result = QtWidgets.QLineEdit(self.layoutWidget_2)
self.face_result.setObjectName("face_result")
self.horizontalLayout_3.addWidget(self.face_result)
self.layoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget_3.setGeometry(QtCore.QRect(460, 380, 261, 51))
self.layoutWidget_3.setObjectName("layoutWidget_3")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.layoutWidget_3)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_4 = QtWidgets.QLabel(self.layoutWidget_3)
self.label_4.setObjectName("label_4")
self.horizontalLayout_4.addWidget(self.label_4)
self.modify_result = QtWidgets.QLineEdit(self.layoutWidget_3)
self.modify_result.setObjectName("modify_result")
self.horizontalLayout_4.addWidget(self.modify_result)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(460, 140, 261, 51))
self.widget.setObjectName("widget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.text_result = QtWidgets.QLineEdit(self.widget)
self.text_result.setObjectName("text_result")
self.horizontalLayout_2.addWidget(self.text_result)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.text_check_btn.setText(_translate("MainWindow", "文本检测"))
self.face_check_btn.setText(_translate("MainWindow", "微表情检测"))
self.modify_check_btn.setText(_translate("MainWindow", "恶意篡改检测"))
self.label.setText(_translate("MainWindow", "文件路径:"))
self.label_3.setText(_translate("MainWindow", "检测结果:"))
self.label_4.setText(_translate("MainWindow", "检测结果:"))
self.label_2.setText(_translate("MainWindow", "检测结果:"))
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.text_check_btn.clicked.connect(self.text_check)
self.face_check_btn.clicked.connect(self.face_check)
self.modify_check_btn.clicked.connect(self.modify_check)
def fun_print(self, sss):
print("来自线程参数 ", sss)
def text_check(self):
filePath = self.file_path_et.text()
self.thread = TextCheckThread(t = 10)
self.thread.customSignal.connect(self.fun_print)
self.thread.start()
print("文本检测", filePath)
self.text_result.setText("检测通过")
def face_check(self):
filePath = self.file_path_et.text()
print("微表情检测", filePath)
self.face_result.setText("11111")
def modify_check(self):
filePath = self.file_path_et.text()
print("恶意篡改检测", filePath)
self.modify_result.setText(filePath)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_Window = MainWindow()
main_Window.show()
sys.exit(app.exec_())