Python PyQt5 QThread将变量传递给main

Python PyQt5 QThread将变量传递给main,python,multithreading,Python,Multithreading,我正在尝试制作一个GUI,它将与串行设备交互 到目前为止,我已经成功地制作了一个非常简单的GUI,可以打开和关闭串行连接 下面是我的代码: import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QMessageBox, QAction, qApp, QPushButton from PyQt5.QtGui import QIcon

我正在尝试制作一个GUI,它将与串行设备交互

到目前为止,我已经成功地制作了一个非常简单的GUI,可以打开和关闭串行连接

下面是我的代码:

import sys
from PyQt5.QtCore import *

from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QMessageBox,      QAction, qApp, QPushButton

from PyQt5.QtGui import QIcon

import serial

ser = serial.Serial()
ser.baudrate = 115200
ser.port = 2

class MainUI(QMainWindow):

def __init__(self):
    super().__init__()
    self.initUI()
    self.workerThread = SerialRead()
    self.connect(self.workerThread, QtCore.SIGNAL("mysignal(QString)"),self.on_change, QtCore.Qt.QueuedConnection)

def initUI(self):
    btn1 = QPushButton('Open Serial', self)
    btn1.clicked.connect(self.openSerial)
    btn1.resize(btn1.sizeHint())
    btn1.move(50,50)

    btn2 = QPushButton('Close Serial', self)
    btn2.clicked.connect(self.closeSerial)
    btn2.resize(btn2.sizeHint())
    btn2.move(50,100)

    exitAction = QAction(QIcon('exit.png'), '&Exit', self)
    exitAction.setShortcut('Alt+F4')
    exitAction.setStatusTip('Exit application')
    exitAction.triggered.connect(self.closeEvent)

    self.statusBar()
    menubar = self.menuBar()
    fileMenu = menubar.addMenu('&File')
    fileMenu.addAction(exitAction)

    self.statusBar().showMessage('Ready')
    self.setGeometry(300,300,250,150)
    self.setWindowTitle('Motor Driver')

    self.show()

def openSerial(self):
    ser.open()
    self.workerThread.start()

def closeSerial(self):
    ser.close()
    self.workerThread.terminate()

def closeEvent(self, event):
    reply = QMessageBox.question(self, 'Message', "Are you sure you want to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
    if reply == QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()

class SerialRead(QThread):
def __init__(self):
    super().__init__()
    self.data = []

def run(self):
    while True:
        x = ser.readline()
        # if x != b'\n':
        #   self.data.append(str(x))
        # else:
        self.emit(QtCore.SIGNAL("mysignal(Qstring)"), (x.decode("utf-8")))
            # self.data = []



if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainUI()
sys.exit(app.exec_())
我的问题是如何将变量从serialRead线程传递到MainUI? 正如您在我的代码中所看到的,我已经尝试了我可以在网上找到的解决方案,这是通过使用:

self.connect(self.workerThread, QtCore.SIGNAL("mysignal(QString)"),self.on_change, QtCore.Qt.QueuedConnection)
self.emit(QtCore.SIGNAL("mysignal(Qstring)"), (x.decode("utf-8")))
在我的MainUI类上,并通过使用:

self.connect(self.workerThread, QtCore.SIGNAL("mysignal(QString)"),self.on_change, QtCore.Qt.QueuedConnection)
self.emit(QtCore.SIGNAL("mysignal(Qstring)"), (x.decode("utf-8")))
在我的线程上

但是,这给了我一个错误: “MainUI”对象没有属性“connect”

那么,我应该如何将变量从线程传递到MainUI呢


自从我上一次接触python已经快2年了,大约2天前我才开始这个项目,但是我还有什么可以改进我的代码的吗?我是否有任何细微或明显的错误?

在PyQt5上,您必须使用