PyQt也给我们提供了这么一个类:QThread
通过继承它然后重写里面的 run()
函数,就可以很容易的新建一个线程,达到多线程的任务。
子线程定义:
class myworkthread(QThread): #声明一个信号,同时返回一个int,什么都可以返回,参数是发送信号时附带参数的数据类型 finishsignal=QtCore.pyqtSignal(int) def __init__(self,t): #t为创建该实例时传递进来的参数,这里需要初始化一下父类的初始化方法,不然会报错 super(myworkthread, self).__init__() self.t=t #重写run函数,也就是子线程的执行函数 def run(self): print('子线程开始') #模拟耗时的程序 time.sleep(self.t) print('子线程结束') #子线程结束后,发送一个信号告诉主线程窗口,并附带参数100 self.finishsignal.emit(100)
创建线程对象,并传入参数10
然后连接子线程的信号和槽函数
self.mywork=myworkthread(10)
self.mywork.finishsignal.connect(self.workend)
开启子线程
self.mywork.start()
通过开启子线程,可以让耗时的程序在子线程中进行,这样ui主线程就不会出现假卡死的情况了。
def workend(self,ins): print('get=%d'%ins) self.open = 1
这个函数要定义一个参数传入接口,因为子线程发过来的信号夹带了一个参数
源代码:
ui界面:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'PGUI.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 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.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout_2.setObjectName("verticalLayout_2") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.comboBox = QtWidgets.QComboBox(self.centralwidget) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(20) self.comboBox.setFont(font) self.comboBox.setObjectName("comboBox") self.horizontalLayout.addWidget(self.comboBox) self.pushButton = QtWidgets.QPushButton(self.centralwidget) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(20) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.verticalLayout_3 = QtWidgets.QVBoxLayout() self.verticalLayout_3.setObjectName("verticalLayout_3") self.label_2 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth()) self.label_2.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(24) self.label_2.setFont(font) self.label_2.setAlignment(QtCore.Qt.AlignCenter) self.label_2.setObjectName("label_2") self.verticalLayout_3.addWidget(self.label_2) self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(20) self.lineEdit.setFont(font) self.lineEdit.setObjectName("lineEdit") self.verticalLayout_3.addWidget(self.lineEdit) self.horizontalLayout_2.addLayout(self.verticalLayout_3) self.verticalLayout_4 = QtWidgets.QVBoxLayout() self.verticalLayout_4.setObjectName("verticalLayout_4") self.label_3 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth()) self.label_3.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(24) self.label_3.setFont(font) self.label_3.setAlignment(QtCore.Qt.AlignCenter) self.label_3.setObjectName("label_3") self.verticalLayout_4.addWidget(self.label_3) self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(20) self.lineEdit_2.setFont(font) self.lineEdit_2.setObjectName("lineEdit_2") self.verticalLayout_4.addWidget(self.lineEdit_2) self.horizontalLayout_2.addLayout(self.verticalLayout_4) self.verticalLayout.addLayout(self.horizontalLayout_2) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth()) self.pushButton_3.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(20) self.pushButton_3.setFont(font) self.pushButton_3.setObjectName("pushButton_3") self.horizontalLayout_3.addWidget(self.pushButton_3) self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(20) self.pushButton_2.setFont(font) self.pushButton_2.setObjectName("pushButton_2") self.horizontalLayout_3.addWidget(self.pushButton_2) self.verticalLayout.addLayout(self.horizontalLayout_3) self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.textBrowser.sizePolicy().hasHeightForWidth()) self.textBrowser.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(16) self.textBrowser.setFont(font) self.textBrowser.setObjectName("textBrowser") self.verticalLayout.addWidget(self.textBrowser) self.verticalLayout_2.addLayout(self.verticalLayout) 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.pushButton.setText(_translate("MainWindow", "开启串口")) self.label_2.setText(_translate("MainWindow", "电压设定值/kV")) self.label_3.setText(_translate("MainWindow", "电流设定值/mA")) self.pushButton_3.setText(_translate("MainWindow", "高压开关")) self.pushButton_2.setText(_translate("MainWindow", "读取电压电流"))
主代码:
from sys import argv,exit import serial from PyQt5 import QtCore from PyQt5.QtCore import QTimer,QThread from PyQt5.QtWidgets import QApplication, QMainWindow from modbus_tk import modbus_rtu import PGUI import time class my_mainwindow(): def __init__(self): # PyQt5中,每个应用程序都必须实例化一个QApplication(): app = QApplication(argv) self.my_MainWindow = QMainWindow() self.my_ui = PGUI.Ui_MainWindow() self.my_ui.setupUi(self.my_MainWindow) #################################################################### self.open = 1 self.timer = QTimer() self.timer.timeout.connect(self.showTime) self.timer.start(1000) self.mywork=myworkthread(10) self.mywork.finishsignal.connect(self.workend) ##################################################################### self.my_MainWindow.show() exit(app.exec_()) def workend(self,ins): print('get=%d'%ins) self.open = 1 def showTime(self): if self.open==1: print('time') self.mywork.start() print('timeover') self.open=0 class myworkthread(QThread): #声明一个信号,同时返回一个int,什么都可以返回,参数是发送信号时附带参数的数据类型 finishsignal=QtCore.pyqtSignal(int) def __init__(self,t): #t为创建该实例时传递进来的参数,这里需要初始化一下父类的初始化方法,不然会报错 super(myworkthread, self).__init__() self.t=t #重写run函数,也就是子线程的执行函数 def run(self): print('子线程开始') #模拟耗时的程序 time.sleep(self.t) print('子线程结束') #子线程结束后,发送一个信号告诉主线程窗口,并附带参数100 self.finishsignal.emit(100) if __name__=="__main__": my_mainwindow()