Как создать эмулятор терминала в PyQt5?

Как создать эмулятор терминала в PyQt5?


Как создать эмулятор терминала в PyQt5?

Я имею в виду чтобы он выглядел и работал похоже на консоль. А не просто исполнял терминальные команды на баше или batch.


import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import subprocess


class MyThread(QThread):
    line_printed = pyqtSignal(str)

    def __init__(self, parent):
        super(MyThread, self).__init__(parent)
        self.cmd = None

    def start_command(self, cmd):
        self.cmd = cmd
        self.start()

    def run(self):
        if self.cmd:
            popen = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, shell=True)
            lines_iterator = iter(popen.stdout.readline, b"")     

            num = 0
            try:
                for line in lines_iterator:
                    num += 1
                    self.line_printed.emit(line.decode('utf-8', errors='ignore') )
                else:
                    if num == 0:
                        self.line_printed.emit("\n`{}` <<--не является внутренней "
                                               "или внешней командой, \n\tисполняемой "
                                               "программой или пакетным файлом.\n".format(self.cmd))
                    else:
                        self.line_printed.emit("\n{}\n".format("-"*50)) 
            except Exception as e:
                # На случай возникновения какой-нибудь ОШИБКИ 
                self.line_printed.emit("\nException as e: --->{}\n".format(e))                 
        else:
            self.line_printed.emit("\nВы не ввели команду для выполнения Cmd!\n")    


class MyDialog(QDialog):
    def __init__(self):
        super().__init__()

        self.textEdit = QTextEdit(self)
        self.textEdit.setText(""" 
        Сюда выводим результаты работы:\n
        subprocess.Popen(self.cmd, stdout=subprocess.PIPE, shell=True) 
        \n
        """)
        self.lineEdit = QLineEdit('py -h', self)
        self.button = QPushButton('START', self, clicked=self.run_thread)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.textEdit)
        self.layout.addWidget(self.lineEdit)        
        self.layout.addWidget(self.button)

        self.thread = MyThread(self)
        self.thread.line_printed.connect(self.handle_line)

    def run_thread(self):
        self.thread.start_command(self.lineEdit.text())

    def handle_line(self, line):
        cursor = self.textEdit.textCursor()
        cursor.movePosition(cursor.End)
        cursor.insertText(line)
        self.textEdit.ensureCursorVisible()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dlg = MyDialog()
    dlg.show()
    app.exec_()
введите сюда описание изображения



Report Page