如何将子进程的标准输入连接到 python 中的 qlineedit [英] how to connect stdin of a subprocess to a qlineedit in python

查看:48
本文介绍了如何将子进程的标准输入连接到 python 中的 qlineedit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a kind of terminal in pyqt5 which you can run a python file from it and it shows you the output.

这是我的代码

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:

and this is test.py:

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

我希望当终端运行文件时,self.inLbl 更改为输入命令的字符串,self.inBar 获取输入并返回输入,而 self.out 写入 self.inLbl 的文本加上输入字符串.

我该怎么做?

I want when the terminal runs the file the self.inLbl changes to the string of input command and self.inBar gets input and returns the input and the self.out writes the text of self.inLbl plus input string.

How can I do that?

推荐答案

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

In this case it is best to use QProcess instead of subprocess.Popen() since it can be easily written using the write() method. On the other hand, test.py must be modified so that it can receive information, input or a similar function must be used:

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+'.')

这篇关于如何将子进程的标准输入连接到 python 中的 qlineedit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆