从pyqt5中的线程获取信息

时间:2018-03-23 09:12:45

标签: python multithreading pyqt pyqt5

我想从线程中获取队列并在该线程之外读取它并执行命令。使用Qt设计器生成GUI。当我尝试下面的代码时,它只打印self.que的None值,它不会更新。下面是我正在尝试的代码。谢谢你的帮助。

from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from mtcnn.mtcnn import MTCNN
from GUI import Ui_MainWindow
import time
import cv2

class Thread(QtCore.QThread):
    changePixmap = QtCore.pyqtSignal(QtGui.QPixmap)
    def __init__(self, parent = None):
        QtCore.QThread.__init__(self, parent=parent)
        self.que = ''
    def run (self):
        capture = cv2.VideoCapture('sample_video.mp4')
        while True:
            check, frame = capture.read()
            cv2.rectangle(frame,(120,80),(520,390),(255,0,0),5)
            convertToQtFormat = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)
            convertToQtFormat = QtGui.QPixmap.fromImage(convertToQtFormat)
            video = convertToQtFormat.scaled(290, 220, QtCore.Qt.KeepAspectRatio)
            self.changePixmap.emit(video)
            face = detector.detect_faces(frame)
            if a_face in face:
                self.que = 'True'

class MainGUI(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
            QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
            self.setupUi(self)
            th = Thread(self)
            th.changePixmap.connect(self.label.setPixmap)
            self.label.show()
            th.start()

                if th.que == "True":
                    print('Yey')

if __name__ == "__main__":
    import sys
    detector = MTCNN()
    app = QtWidgets.QApplication(sys.argv)
    w = MainGUI()
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

如果您想将信息从一个线程发送到另一个线程,您应该使用信号,在您的情况下创建一个发送docker.for.mac.internal.host并将其连接到插槽的信号:

str