PyQt无法从QProcess程序读取Stdout

时间:2017-07-24 04:37:26

标签: python python-2.7 pyqt pyqt4 qprocess

该应用程序是Windows命令行程序的GUI包装程序。按钮const Public = ({ loggingIn, authenticated, component: Component, ...rest }) => ( <Route {...rest} render={(props) => { return !authenticated ? (<Component loggingIn={ loggingIn } authenticated={ authenticated } { ...rest } />) : (<Redirect to="/" />); }} /> ); 创建一个运行the CLI program的新流程。 CLI程序的输出打印在starBbtn

问题: CLI输出中没有任何内容似乎插入QTextEdit。使用Task Manaager,我们可以看到该程序确实正在运行。

但是,当我们开始QTextEdit运行QProcess时,stdout输出已成功插入ping

直接运行CLI程序会显示许多打印到控制台的内容。

使用QTextEdit运行也会将标准输出打印到控制台。

subprocess.Popen

我们怎样才能将import subprocess cmd = 'someApp.exe' p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE) someApp.exe的标准输出到QProcess

QTextEdit无法输出

enter image description here

someApp.exe

的输出

enter image description here

ping

用此覆盖import sys from PyQt4 import QtGui, QtCore class MainWindow(QtGui.QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.initUI() def dataReady(self): cursorOutput = self.output.textCursor() cursorOutput.movePosition(cursorOutput.End) # Read stdout from child process processStdout = str(self.process.readAll()) # Update self.output cursorOutput.insertText(processStdout) self.output.ensureCursorVisible() def startProcess(self): # self.process.start('ping', ['127.0.0.1']) self.process.start('someApp.exe') def initUI(self): # Elements self.startBtn = QtGui.QPushButton('Start') self.startBtn.clicked.connect(self.startProcess) self.output = QtGui.QTextEdit(self) # Layout layout = QtGui.QGridLayout() layout.addWidget(self.startBtn, 0, 0) layout.addWidget(self.output, 1, 0) centralWidget = QtGui.QWidget() centralWidget.setLayout(layout) self.setCentralWidget(centralWidget) # QProcess object for external app self.process = QtCore.QProcess(self) self.process.readyRead.connect(self.dataReady) self.process.started.connect(lambda: self.startBtn.setEnabled(False)) self.process.finished.connect(lambda: self.startBtn.setEnabled(True)) def main(): app = QtGui.QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_()) if __name__ == '__main__': main() 用于复制问题

config.txt

0 个答案:

没有答案