Skip to content Skip to sidebar Skip to footer

Printing Output From Console Live To Qtextedit

so I am trying to (live) update a QTextEdit with shell output as such: txtDirb = QTextEdit() dirb_command = 'dirb' + ' ' + url p = subprocess.Popen([dirb_command], stdout=subproces

Solution 1:

Do not use subprocess.Popen() since it is blocking and it only gives you the result at the end of the execution, instead use QProcess:

import sys

from PyQt5 import QtCore, QtWidgets


classWidget(QtWidgets.QWidget):
    def__init__(self, parent=None):
        super().__init__(parent)

        self.process = QtCore.QProcess(self)
        self.process.setProgram("dirb")
        self.process.setProcessChannelMode(QtCore.QProcess.MergedChannels)

        self.lineedit = QtWidgets.QLineEdit("http://webscantest.com")
        self.button = QtWidgets.QPushButton("Start")
        self.textedit = QtWidgets.QTextEdit(readOnly=True)

        lay = QtWidgets.QGridLayout(self)
        lay.addWidget(self.lineedit, 0, 0)
        lay.addWidget(self.button, 0, 1)
        lay.addWidget(self.textedit, 1, 0, 1, 2)

        self.button.clicked.connect(self.on_clicked)
        self.process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
        self.process.finished.connect(self.on_finished)

    @QtCore.pyqtSlot()defon_clicked(self):
        if self.button.text() == "Start":
            self.textedit.clear()
            self.process.setArguments([self.lineedit.text()])
            self.process.start()
            self.button.setText("Stop")
        elif self.button.text() == "Stop":
            self.process.kill()

    @QtCore.pyqtSlot()defon_readyReadStandardOutput(self):
        text = self.process.readAllStandardOutput().data().decode()
        self.textedit.append(text)

    @QtCore.pyqtSlot()defon_finished(self):
        self.button.setText("Start")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

enter image description here

Post a Comment for "Printing Output From Console Live To Qtextedit"