如何在一个进程中多次启动 pyqt GUI?

Iva*_*van 5 python pyqt pyqtgraph pyqt5

如何构建代码以在进程中连续多次运行 pyqt GUI?

(特别是pyqtgraph,如果相关的话)

上下文

在测量设备上执行长时间运行数据捕获的 python 脚本(一个大循环)。在每次捕获迭代期间,都会出现一个新的 GUI,并向用户显示来自测量设备的实时数据,同时主捕获代码正在运行。

我想做这样的事情:

for setting in settings:
  measurement_equipment.start(setting)
  gui = LiveDataStreamGUI(measurement_equipment)
  gui.display()
  measurement_equipment.capture_data(300) #may take hours
  gui.close()
Run Code Online (Sandbox Code Playgroud)

主要问题

我希望数据捕获代码成为主线程。然而 pyqt 似乎不允许这种架构,因为它app.exec_()是一个阻塞调用,允许每个进程只创建一次 GUI(例如,在gui.display()上面)。

Ism*_*sma 5

应用程序是在一个或多个前台线程上运行的可执行进程,每个前台线程还可以启动后台线程来执行并行操作或操作,而不会阻塞调用线程。应用程序将在所有前台线程结束后终止,因此,您至少需要一个前台线程,在您的情况下,该线程是在您调用该app.exec_()语句时创建的。在 GUI 应用程序中,这是 UI 线程,您应该在其中创建和显示主窗口和任何其他 UI 小部件。当所有小部件关闭时,Qt 将自动终止您的应用程序进程。

恕我直言,您应该尽量遵循上述正常流程,工作流程如下:

启动应用程序 > 创建主窗口 > 为每个计算启动一个后台线程 > 将进度发送到 UI 线程 > 每次计算完成后在窗口中显示结果 > 关闭所有窗口 > 结束应用程序

此外,您应该使用ThreadPool以确保不会耗尽资源。

这是一个完整的例子:

import sys
import time
import PyQt5

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QRunnable, pyqtSignal, QObject
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QDialog


class CaptureDataTaskStatus(QObject):
    progress = pyqtSignal(int, int)  # This signal is used to report progress to the UI thread.
    captureDataFinished = pyqtSignal(dict)  # Assuming your result is a dict, this can be a class, a number, etc..


class CaptureDataTask(QRunnable):
    def __init__(self, num_measurements):
        super().__init__()

        self.num_measurements = num_measurements
        self.status = CaptureDataTaskStatus()

    def run(self):
        for i in range(0, self.num_measurements):
            # Report progress
            self.status.progress.emit(i + 1, self.num_measurements)
            # Make your equipment measurement here
            time.sleep(0.1) # Wait for some time to mimic a long action

        # At the end you will have a result, for example
        result = {'a': 1, 'b': 2, 'c': 3}

        # Send it to the UI thread
        self.status.captureDataFinished.emit(result)


class ResultWindow(QWidget):
    def __init__(self, result):
        super().__init__()

        # Display your result using widgets...
        self.result = result

        # For this example I will just print the dict values to the console
        print('a: {}'.format(result['a']))
        print('b: {}'.format(result['b']))
        print('c: {}'.format(result['c']))


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.result_windows = []

        self.thread_pool = QtCore.QThreadPool().globalInstance()

        # Change the following to suit your needs (I just put 1 here so you can see each task opening a window while the others are still running)
        self.thread_pool.setMaxThreadCount(1)

        # You could also start by clicking a button, menu, etc..
        self.start_capturing_data()

    def start_capturing_data(self):
        # Here you start data capture tasks as needed (I just start 3 as an example)
        for setting in range(0, 3):
            capture_data_task = CaptureDataTask(300)
            capture_data_task.status.progress.connect(self.capture_data_progress)
            capture_data_task.status.captureDataFinished.connect(self.capture_data_finished)
            self.thread_pool.globalInstance().start(capture_data_task)


    def capture_data_progress(self, current, total):
        # Update progress bar, label etc... for this example I will just print them to the console
        print('Current: {}'.format(current))
        print('Total: {}'.format(total))

    def capture_data_finished(self, result):
        result_window = ResultWindow(result)
        self.result_windows.append(result_window)
        result_window.show()


class App(QApplication):
    """Main application wrapper, loads and shows the main window"""

    def __init__(self, sys_argv):
        super().__init__(sys_argv)

        self.main_window = MainWindow()
        self.main_window.show()


if __name__ == '__main__':
    app = App(sys.argv)   
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)


Jos*_*aCS 4

如果你希望你的 GUI 保持实时更新并且不被冻结,你有两种主要方法可以做到:

  1. QApplication.processEvents()在耗时的函数中调用时时刷新 GUI 。
  2. 创建一个单独的线程(我的意思是,QThread),在其中运行耗时的函数

我个人倾向于选择后一种方式。是一个很好的教程,帮助您开始了解如何在 Qt 中进行多线程处理。

看看你的代码:

...
gui.display()
measurement_equipment.capture_data(300) #may take hours
gui.close()
...
Run Code Online (Sandbox Code Playgroud)

看来你app.exec_在里面打电话gui.display。您很可能必须解耦两个函数并在调用app.exec_之外gui.display和之后进行调用capture_data。您还必须将finished新线程的信号连接到gui.close. 它将是这样的:

...
gui.display() # dont call app.exec_ here
thread = QThread.create(measurement_equipment.capture_data, 300)
thread.finished.connect(gui.close)
app.exec_()
...
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助你,不要迟到!