Python 如何在pyQt中打印控制台输出
我需要在Pyqt窗口中显示adb日志,我试着这样做。当我单击按钮时,会调用log_显示功能,该功能将控制台输出设置为textBrowser。我试图使用subprocess,但它没有帮助,窗口只是冻结,没有响应。怎么做?也许我需要使用新的线程Python 如何在pyQt中打印控制台输出,python,pyqt,adb,Python,Pyqt,Adb,我需要在Pyqt窗口中显示adb日志,我试着这样做。当我单击按钮时,会调用log_显示功能,该功能将控制台输出设置为textBrowser。我试图使用subprocess,但它没有帮助,窗口只是冻结,没有响应。怎么做?也许我需要使用新的线程 def log_display(self): result = subprocess.run('adb logcat *:I', stdout=subprocess.PIPE) self.textBrowser.setText(subproc
def log_display(self):
result = subprocess.run('adb logcat *:I', stdout=subprocess.PIPE)
self.textBrowser.setText(subprocess.run('result.stdout'))
您必须使用QProcess,因为它会启动应用程序,但不会阻止GUI,因为它会通过以下信号通知日志:
from PyQt5 import QtCore, QtGui, QtWidgets
class LogView(QtWidgets.QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.setReadOnly(True)
self._process = QtCore.QProcess()
self._process.readyReadStandardOutput.connect(self.handle_stdout)
self._process.readyReadStandardError.connect(self.handle_stderr)
def start_log(self, program, arguments=None):
if arguments is None:
arguments = []
self._process.start(program, arguments)
def add_log(self, message):
self.appendPlainText(message.rstrip())
def handle_stdout(self):
message = self._process.readAllStandardOutput().data().decode()
self.add_log(message)
def handle_stderr(self):
message = self._process.readAllStandardError().data().decode()
self.add_log(message)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = LogView()
w.resize(640, 480)
w.show()
w.start_log("adb", ["logcat", "*:I"])
sys.exit(app.exec_())
不,你不一定需要一根线。但是这部分没有意义,self.textBrowser.setText(subprocess.run('result.stdout'))