如何解决如何在pyQt中打印控制台输出
我需要在Pyqt窗口中显示adb日志,我试图这样做。当我单击按钮时,将调用log_display函数,该函数将控制台输出设置为textBrowser。我尝试使用子进程,但没有帮助,窗口只是冻结,没有响应。怎么做呢?也许我需要为此使用新线程?
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_())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。