我有读取10 uart并在pyqt5 gui中打印数据的并行线程。但是我面临的问题是,当所有10个作业完成但不是立即完成时,那些groupbox会更新。
编辑:要实现并行性,我使用的是joblib而不是pyqt线程。
我想要实现的是更改groubox属性的状态,例如标签,按钮文本和颜色,但是我找不到如何触发groupbox信号的好例子。
我需要发出单个标签,组框中的按钮还是可以作为一个组框对象发出?
这是一个简约代码,其中只有两个groupbx。有人可以帮忙如何从groubox发出信号并捕获该更改,即以下代码中的标签,按钮文本和颜色的状态?
下面的示例更新groubox没有任何问题,但实际上每个g1方法都是uart,它阻止了调用。我等到所有作业都完成后才能在GUI上看到更改。
代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, pyqtSignal
class Ui_MainWindow(object):
changedValue = pyqtSignal(int)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(100, 140, 120, 141))
self.groupBox.setObjectName("groupBox")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_3.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 80, 113, 32))
self.pushButton.setObjectName("pushButton")
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(230, 140, 120, 141))
self.groupBox_2.setObjectName("groupBox_2")
self.pushButton_4 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_4.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_5.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_5.setObjectName("pushButton_5")
self.label_2 = QtWidgets.QLabel(self.groupBox_2)
self.label_2.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.groupBox.update)
self.pushButton.clicked.connect(self.groupBox_2.update)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MainWindow", "UART1"))
self.pushButton_2.setText(_translate("MainWindow", "Test1"))
self.pushButton_3.setText(_translate("MainWindow", "Test2"))
self.label.setText(_translate("MainWindow", "IMEI"))
self.pushButton.setText(_translate("MainWindow", "START"))
self.groupBox_2.setTitle(_translate("MainWindow", "UART2"))
self.pushButton_4.setText(_translate("MainWindow", "Test1"))
self.pushButton_5.setText(_translate("MainWindow", "Test2"))
self.label_2.setText(_translate("MainWindow", "IMEI"))
#Setting a connection here
self.pushButton.clicked.connect(self.g1)
self.pushButton.clicked.connect(self.g2)
def g1(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Running"))
imei = "12345"
self.label.setText(imei)
self.changedValue.emit(imei)
self.pushButton_2.setText(_translate("MainWindow", "Pass"))
self.pushButton_3.setText(_translate("MainWindow", "FAIL"))
self.pushButton_3.setStyleSheet("background-color: rgb(0, 255, 0);")
def g2(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Done"))
imei = "12345"
self.label_2.setText("12345")
self.changedValue.emit(imei)
self.pushButton_4.setText(_translate("MainWindow", "FAIL"))
self.pushButton_4.setStyleSheet("background-color: rgb(0, 255, 0);")
self.pushButton_5.setText(_translate("MainWindow", "Pass"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
首先,我建议您阅读文档中的Using the Generated Code。这篇文章中的PyQt建议不要修改Qt Designer生成的代码,因为它不是窗口小部件,它只是一个用于填充窗口小部件的类,相反,最好根据所选表单创建另一个类并在其中实现逻辑,因为例如您的代码有一个严重错误:信号只能在像小部件的QObject中创建,而Ui_MainWindow则不能。
考虑到上述情况,解决方案是:
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.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(100, 140, 120, 141))
self.groupBox.setObjectName("groupBox")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_3.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 80, 113, 32))
self.pushButton.setObjectName("pushButton")
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(230, 140, 120, 141))
self.groupBox_2.setObjectName("groupBox_2")
self.pushButton_4 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_4.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_5.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_5.setObjectName("pushButton_5")
self.label_2 = QtWidgets.QLabel(self.groupBox_2)
self.label_2.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.groupBox.update)
self.pushButton.clicked.connect(self.groupBox_2.update)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MainWindow", "UART1"))
self.pushButton_2.setText(_translate("MainWindow", "Test1"))
self.pushButton_3.setText(_translate("MainWindow", "Test2"))
self.label.setText(_translate("MainWindow", "IMEI"))
self.pushButton.setText(_translate("MainWindow", "START"))
self.groupBox_2.setTitle(_translate("MainWindow", "UART2"))
self.pushButton_4.setText(_translate("MainWindow", "Test1"))
self.pushButton_5.setText(_translate("MainWindow", "Test2"))
self.label_2.setText(_translate("MainWindow", "IMEI"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
changedValue = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#Setting a connection here
self.pushButton.clicked.connect(self.g1)
self.pushButton.clicked.connect(self.g2)
@QtCore.pyqtSlot()
def g1(self):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Running"))
imei = "12345"
self.label.setText(imei)
self.changedValue.emit(imei)
self.pushButton_2.setText("Pass")
self.pushButton_3.setText(_translate("MainWindow", "FAIL"))
self.pushButton_3.setStyleSheet("background-color: rgb(0, 255, 0);")
@QtCore.pyqtSlot()
def g2(self):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Done"))
imei = "12345"
self.label_2.setText("12345")
self.changedValue.emit(imei)
self.pushButton_4.setText(_translate("MainWindow", "FAIL"))
self.pushButton_4.setStyleSheet("background-color: rgb(0, 255, 0);")
self.pushButton_5.setText(_translate("MainWindow", "Pass"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())