如何将子进程的stdin连接到python中的qlineedit

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

我正在pyqt5中构建一种终端,您可以从中运行python文件,并向您显示输出。

这是我的代码

import sys
from subprocess import Popen,PIPE

from PyQt5.QtWidgets import (QApplication,QWidget,QVBoxLayout,
                             QHBoxLayout,QPlainTextEdit,QLabel,
                             QLineEdit)

class Terminal(QWidget):
    def __init__(self):
        super().__init__()
        lay = QHBoxLayout()
        layout = QVBoxLayout()
        self.setLayout(layout)

        self.out = QPlainTextEdit()
        self.inLbl = QLabel('')
        self.inBar = QLineEdit()

        lay.addWidget(self.inLbl)
        lay.addWidget(self.inBar)

        layout.addWidget(self.out)
        layout.addLayout(lay)
    def runFile(self,url):
        self.out.clear()
        p = Popen(['python',url],stdout = PIPE,stderr = PIPE)
        stdout,stderr = p.communicate()
        err = stderr.decode()

        self.out.insertPlainText(stdout.decode())

        if err != '':
            self.out.insertPlainText('\n'+stderr.decode())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Terminal()
    window.runFile('test.py')
    window.show()
    sys.exit(app.exec_())

这是test.py:

print('Hello '+name+'.')

我想在终端运行文件时,self.inLbl更改为输入命令的字符串,self.inBar获取输入并返回输入,self.out编写self.inLbl加上输入字符串的文本。我该怎么办?

python pyqt subprocess pyqt5 python-3.8
1个回答
0
投票

在这种情况下,最好使用QProcess而不是subprocess.Popen(),因为可以使用write()方法轻松编写它。另一方面,必须修改test.py以便它可以接收信息,输入或必须使用类似的功能:

import sys

from PyQt5 import QtCore, QtWidgets


class Terminal(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.out = QtWidgets.QPlainTextEdit(readOnly=True)
        self.inBar = QtWidgets.QLineEdit()

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.out)
        layout.addWidget(self.inBar)

        self.process = QtCore.QProcess(self)
        self.process.setProgram(sys.executable)
        self.process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
        self.process.readyReadStandardError.connect(self.on_readyReadStandardError)
        self.inBar.editingFinished.connect(self.on_editingFinished)

    def runFile(self, url):
        self.process.setArguments([url])
        self.process.start()

    @QtCore.pyqtSlot()
    def on_readyReadStandardOutput(self):
        out = self.process.readAllStandardOutput().data().decode()
        self.out.insertPlainText(out)

    @QtCore.pyqtSlot()
    def on_readyReadStandardError(self):
        err = self.process.readAllStandardError().data().decode()
        self.out.insertPlainText("\n" + err)

    @QtCore.pyqtSlot()
    def on_editingFinished(self):
        self.process.write(self.inBar.text().encode() + b"\n")
        self.inBar.clear()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Terminal()
    window.runFile("test.py")
    window.show()
    sys.exit(app.exec_())

test.py

while True:
    name = input()
    print('Hello '+name+'.')
© www.soinside.com 2019 - 2024. All rights reserved.