使用QThreading和QProcess进行GUI冻结

问题描述 投票:0回答:1

我正在尝试编写一些软件,该软件将处理从某些晶体学实验中收集到的大量图像。数据处理涉及以下步骤:

  1. 用户输入以确定要一起批处理的图像数。
  2. 选择包含图像的目录,并计算图像总数。
  3. 嵌套的for循环用于将图像批处理在一起,并为每个使用批处理文件处理的批处理构造命令和参数。

以下代码可用于模拟使用QThread和QProcess描述的过程:

# This Python file uses the following encoding: utf-8
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import test
import time

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui=test.Ui_test()
        self.ui.setupUi(self)
        self.ui.pushButton_startThread.clicked.connect(self.startTestThread)

    def startTestThread(self):
        self.xValue = self.ui.lineEdit_x.text() #Represents number of batches
        self.yValue = self.ui.lineEdit_y.text() #Represents number of images per batch
        runTest = testThread(self.xValue, self.yValue) #Creates an instance of testThread
        runTest.start() #Starts the instance of testThread

class testThread(QThread):
    def __init__(self, xValue, yValue):
        super().__init__()
        self.xValue = xValue
        self.yValue = yValue

    def __del__(self):
        self.wait()

    def run(self):
        for x in range(int(self.xValue)): #For loop to iterate througeach batch
            print(str(x) + "\n")
            for y in range(int(self.yValue)): #For loop to iterate through each image in each batch
                print(str(y) + "\n")
            print(y)
            process = QProcess(self) #Creates an instance of Qprocess
            process.startDetached("test.bat") #Runs test.bat

    def stop(self):
        self.terminate()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle("Fusion")
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

test.bat内容:

@ECHO OFF
ECHO this is a test

GUI包含两个用于xValue和yValue的用户输入以及一个用于启动线程的按钮。例如,一个实验产生了150,000张图像,需要分500批进行处理。每批次需要处理300张图像。您可以为xValue输入500,为yValue输入300。有两个问题:

  1. GUI冻结,因此如果需要,我无法停止该过程。我以为运行线程可以防止这种情况。
  2. 我收到以下错误:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is testThread(0x1a413f3c690), parent's thread is QThread(0x1a4116cb7a0), current thread is testThread(0x1a413f3c690)

我相信此错误是通过嵌套的for循环生成多个QProcesses的结果,但我不确定。

反正有没有办法使GUI停止冻结并避免产生的错误?

非常感谢您的帮助!保持安全!

我正在尝试编写一些软件,该软件将处理从某些晶体学实验中收集到的大量图像。数据处理涉及以下步骤:用户输入以确定...

python pyqt5 python-multithreading qthread qprocess
1个回答
0
投票

说明

© www.soinside.com 2019 - 2024. All rights reserved.