Python PyQt5,如何重新启动通过线程运行的操作

Python PyQt5,如何重新启动通过线程运行的操作,python,pyqt5,python-multithreading,Python,Pyqt5,Python Multithreading,所以我一直在尝试创建一个GUI,它可以在多个线程上执行每个操作。每个操作实际上都有循环在其上运行,因此我决定使用线程来同时运行每个操作 下面是一些描述工作流程的代码 下面有一个名为cvfeed1的函数,当使用(self.button_3.clicked.connect(self.cvfeed1))在GUI上按下按钮时,该函数将启动一个线程。当线程启动时,它调用一个名为color_detect的函数。我面临的问题是,我只能使用此线程一次,即我只能单击按钮一次。当函数中的操作完成后,我再次单击按钮,

所以我一直在尝试创建一个GUI,它可以在多个线程上执行每个操作。每个操作实际上都有循环在其上运行,因此我决定使用线程来同时运行每个操作

下面是一些描述工作流程的代码

下面有一个名为cvfeed1的函数,当使用(self.button_3.clicked.connect(self.cvfeed1))在GUI上按下按钮时,该函数将启动一个线程。当线程启动时,它调用一个名为color_detect的函数。我面临的问题是,我只能使用此线程一次,即我只能单击按钮一次。当函数中的操作完成后,我再次单击按钮,程序崩溃。我需要做什么才能再次使线程工作

class MyWindowClass(QMainWindow, form_class):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)

        self.startButton.clicked.connect(self.start_clicked)
        self.pushButton.clicked.connect(self.printer)

        # Here is the command when the button is clicked.

        self.pushButton_3.clicked.connect(self.cvfeed1)

        self.window_width = self.ImgWidget.frameSize().width()
        self.window_height = self.ImgWidget.frameSize().height()
        self.ImgWidget = OwnImageWidget(self.ImgWidget)       

        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.update_frame)
        self.timer.start(1)

    # This function starts the detect_1_thread

    def cvfeed1(self):

        detect_1_thread.start()
        #self.pushButton_3.setEnabled(False)

    def printer(self):
        global running1
        running1=True
        printer_thread.start()

    def start_clicked(self):
        global running
        running = True
        capture_thread.start()
        self.startButton.setEnabled(False)
        self.startButton.setText('Starting...')


    def update_frame(self):
        if not q.empty():
            self.startButton.setText('Camera is live')
            frame = q.get()
            img = frame["img"]

            img_height, img_width, img_colors = img.shape
            scale_w = float(self.window_width) / float(img_width)
            scale_h = float(self.window_height) / float(img_height)
            scale = min([scale_w, scale_h])

            if scale == 0:
                scale = 1

            img = cv2.resize(img, None, fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            height, width, bpc = img.shape
            bpl = bpc * width
            image = QtGui.QImage(img.data, width, height, bpl, QtGui.QImage.Format_RGB888)
            self.ImgWidget.setImage(image)

    def closeEvent(self, event):
        global running
        running = False



capture_thread = threading.Thread(target=grab, args = (0, q, 1920, 1080, 30))
printer_thread = threading.Thread(target=printx)
detect_1_thread=threading.Thread(target=colour_detect)

app = QApplication(sys.argv)
w = MyWindowClass(None)
w.setWindowTitle('GUI')
w.show()
app.exec_()

您不能多次启动线程:它是一个操作,而不是一台可以被其他代理(重新)使用的机器。您可以使用相同的函数启动另一个线程,但如果不与第一个线程同步,则可能会导致两个线程同时运行,这可能是一件坏事

如果要进行同步,那么最好让一个线程无限期地运行,并在适当的时间表上发布对它的处理请求。该计划可能允许抢占当前操作,或者可能只允许一个请求在当前操作之后排队,或者其他任何情况