readyReadStandardOutput 信号不起作用
Posted
技术标签:
【中文标题】readyReadStandardOutput 信号不起作用【英文标题】:readyReadStandardOutput signal does not work 【发布时间】:2018-07-14 16:10:12 【问题描述】:我正在尝试使用 PyQt5 QProcess 类从子进程获取标准输出输出。如果我使用 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】:当您进行迭代时,您正在使用相同的QProcess
消除先前不正确的任务,您必须做的是为您启动的每个.py
文件创建一个新的QProcess
,另一方面返回结果你必须创建一个信号,因为它不知道你在什么时候得到结果。
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_())
【讨论】:
以上是关于readyReadStandardOutput 信号不起作用的主要内容,如果未能解决你的问题,请参考以下文章