记录一下在项目中遇到的问题。
在UI界面中,一般会有一些控件比如PushButton、Qlabel等用来触发某一个事件,若该事件十分耗时,那么将会阻塞主程序,造成UI界面出现卡顿或者未响应的问题,因此我们必须要使用多线程来解决。
一般按照常规写多线程,即直接传入函数,启动多线程,传入参数这个方法,在PyQt5界面刷新中实现不了,只能用PyQt5中的QThread来写多线程。
问题描述:
编写的上位机程序中,当触发界面上的按钮给下位机发送控制命令时,摄像头画面停止不动,下位机先执行控制命令,待下位机执行完毕,上位机画面迅速显示。问题很显然,程序是并发运行,不能将画面和控制命令同时进行,在一方运行的同时,另一个程序阻塞等待。
解决办法:
加入多线程机制。
由于项目原因,特将个人程序进行删改,不影响运行效果。
注意:本实例需要用到两个库:pyqt5和opencv。
安装方法见此链接:https://blog.csdn.net/zcs_xueli/article/details/105737403
下面这个程序是没有加入多线程的程序,小伙伴们可以直接在电脑运行,感受一下界面卡顿的效果。
下面这个程序是label0.py。 .ui文件转成的.py文件。本实例的程序将界面和逻辑分开编写
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(648, 734)
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 116, 170))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 116, 170))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(51, 153, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, brush)
MainWindow.setPalette(palette)
font = QtGui.QFont()
font.setPointSize(10)
MainWindow.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/Administrator/Desktop/logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
MainWindow.setAutoFillBackground(False)
MainWindow.setStyleSheet("background-color: rgb(191, 191, 191);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_9 = QtWidgets.QLabel(self.centralwidget)
self.label_9.setGeometry(QtCore.QRect(180, 0, 271, 51))
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.label_9.setFont(font)
self.label_9.setStyleSheet("background-color: rgb(191, 191, 191);")
self.label_9.setObjectName("label_9")
self.groupBox_5 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_5.setGeometry(QtCore.QRect(0, 60, 631, 611))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(191, 191, 191))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
pal