gpt4 book ai didi

python - 将输出从控制台实时打印到 QTextEdit

转载 作者:行者123 更新时间:2023-12-01 06:33:59 26 4
gpt4 key购买 nike

所以我尝试使用 shell 输出(实时)更新 QTextEdit:

txtDirb = QTextEdit()
dirb_command = "dirb" + " " + url
p = subprocess.Popen([dirb_command], stdout=subprocess.PIPE, shell=True)
out = p.stdout.read()
txtDirb.append(str(out)) # buggy!

当然,这不会实时更新,而是等待整个命令执行,然后填充QTextEdit。有没有办法实现实时更新?

谢谢。

最佳答案

不要使用subprocess.Popen(),因为它是阻塞的,并且只会在执行结束时给出结果,而是使用QProcess:

import sys

from PyQt5 import QtCore, QtWidgets


class Widget(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()
def on_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()
def on_readyReadStandardOutput(self):
text = self.process.readAllStandardOutput().data().decode()
self.textedit.append(text)

@QtCore.pyqtSlot()
def on_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

关于python - 将输出从控制台实时打印到 QTextEdit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59759703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com