readyReadStandardOutput信号不起作用

时间:2018-07-14 16:10:13

标签: python pyqt pyqt5 qprocess

我正在尝试使用PyQt5 QProcess类从子进程获取stdout输出。如果我使用waitForFinished(),则QMainWindow被冻结。但是,尽管过程已启动,但信号readyReadStandardOutput无法正常工作。这是我的代码:

startup.py

from PyQt5.QtCore import QDir, QObject, QProcess
import settings_store
import os.path
import sys


class Getter(QObject):
    process = QProcess()
    output = ''

    def __init__(self):
    super().__init__()
    self.process.setProcessChannelMode(QProcess.MergedChannels)
    self.process.readyReadStandardOutput.connect(self.ready)

    def start(self):
        templates_folder = QDir(templates_path())
        for template in templates_folder.entryList(['*.py'], QDir.Files):
            self.process.start(sys.executable, [file_(template), 'on_startup'])

    def ready(self):
        self.output = bytes(self.process.readAllStandardOutput()).decode('UTF-8').strip()
        print(self.output, 'yeah')


# constants
def templates_path():
    return os.path.join(settings_store.settings_path(), settings_store.directory(), 'templates')


def file_(template):
    return os.path.join(templates_path(), template)

greetings_template.py-模板文件夹中的文件

import sys
import time


def on_startup():
    print('Can we wait a bit?')
    time.sleep(5)
    count = 0
    while count < 5:
        time.sleep(1)
        print("waiting now too")
        count += 1
    print('jeff.find_reagent hello')


if sys.argv[1] == 'on_startup':
    on_startup()

1 个答案:

答案 0 :(得分:0)

进行迭代时,您将使用相同的QProcess来消除上一个不正确的任务,那么您必须为启动的每个QProcess文件创建一个新的.py ,另一方面,要返回结果,您必须创建一个信号,因为它不知道您在何时获得结果。

startup.py

import sys
import os.path

from PyQt5.QtCore import QDir, QObject, QProcess, pyqtSlot, pyqtSignal

import settings_store


class Getter(QObject):
    resultChanged = pyqtSignal(str)
    def start(self):
        templates_folder = QDir(templates_path())
        for template in templates_folder.entryList(['*.py'], QDir.Files):
            process = QProcess(self)
            process.setProcessChannelMode(QProcess.MergedChannels)
            process.readyReadStandardOutput.connect(self.ready)
            process.start(sys.executable, [file_(template), 'on_startup'])

    @pyqtSlot()
    def ready(self):
        process = self.sender()
        output = bytes(process.readAllStandardOutput()).decode('UTF-8').strip()
        print(output, 'yeah')
        self.resultChanged.emit(output)


# constants
def templates_path():
    return os.path.join(settings_store.settings_path(), settings_store.directory(), 'templates')

def file_(template):
    return os.path.join(templates_path(), template)

main.py

from PyQt5.QtWidgets import *

from startup import Getter

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        button = QPushButton("Press me")
        textEdit = QTextEdit()

        widget = QWidget()
        self.setCentralWidget(widget)
        lay = QVBoxLayout(widget)
        lay.addWidget(button)
        lay.addWidget(textEdit)
        getter = Getter(self)
        getter.resultChanged.connect(textEdit.append)
        button.clicked.connect(getter.start)

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())