实时重定向控制台输出到pyqt5 gui
Redirect console output to pyqt5 gui in real time
我想在使用 PyQt5
的 gui 中 实时 重定向 python 程序的控制台输出
我的意思是,在 python 程序执行的每个输出中,gui 会立即显示此输出
我使用的代码在完成整个程序后只显示日志,并没有解决实时问题
你能帮帮我吗
在我的代码下方:
from PyQt5 import QtGui,QtCore
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap
class gui(QMainWindow):
def __init__(self):
super(gui, self).__init__()
self.initUI()
def dataReady(self):
cursor = self.output.textCursor()
cursor.movePosition(cursor.End)
cursor.insertText(str(self.process.readAll()))
self.output.ensureCursorVisible()
def callProgram(self):
# run the process
# `start` takes the exec and a list of arguments
self.process.start('python',['Robot.py'])
def initUI(self):
# Layout are better for placing widgets
layout = QHBoxLayout()
self.runButton = QPushButton('Run')
self.runButton.clicked.connect(self.callProgram)
self.output = QTextEdit()
self.setGeometry(100, 60, 1000, 800)
layout.addWidget(self.output)
layout.addWidget(self.runButton)
centralWidget = QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
# QProcess object for external app
self.process = QtCore.QProcess(self)
# QProcess emits `readyRead` when there is data to be read
self.process.readyRead.connect(self.dataReady)
# Just to prevent accidentally running multiple times
# Disable the button when process starts, and enable it when it finishes
self.process.started.connect(lambda: self.runButton.setEnabled(False))
self.process.finished.connect(lambda: self.runButton.setEnabled(True))
#Function Main Start
def main():
app =QApplication(sys.argv)
ui=gui()
ui.show()
sys.exit(app.exec_())
#Function Main END
if __name__ == '__main__':
main() ```
当您将 python 输出重定向到文件或管道时,出于性能原因,它会被缓冲。您需要使用 -u
cli 参数 或 编写 运行 脚本,像这样刷新每个打印输出:print(something, flush=True)
。有关详细信息,请参阅 this question。
self.process.start('python',['-u','Robot.py'])
完整的解决方案,感谢@mugiseybrows 和@eyllanesc
它解决了两个问题:第一个是在 gui 中重定向 python 程序的输出,第二个是在独立的行中显示每个输出
import sys
from PyQt5 import QtGui,QtCore
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap
class gui(QMainWindow):
def __init__(self):
super(gui, self).__init__()
self.initUI()
def dataReady(self):
cursor = self.output.textCursor()
cursor.movePosition(cursor.End)
#cursor.insertText(str(self.process.readAll()))
cursor.insertText(self.process.readAll().data().decode())
self.output.ensureCursorVisible()
def callProgram(self):
# run the process
# `start` takes the exec and a list of arguments
self.process.start('python',['-u','Robot.py'])
def initUI(self):
# Layout are better for placing widgets
layout = QHBoxLayout()
self.runButton = QPushButton('Run')
self.runButton.clicked.connect(self.callProgram)
self.output = QTextEdit()
self.setGeometry(100, 60, 1000, 800)
layout.addWidget(self.output)
layout.addWidget(self.runButton)
centralWidget = QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
# QProcess object for external app
self.process = QtCore.QProcess(self)
# QProcess emits `readyRead` when there is data to be read
self.process.readyRead.connect(self.dataReady)
# Just to prevent accidentally running multiple times
# Disable the button when process starts, and enable it when it finishes
self.process.started.connect(lambda: self.runButton.setEnabled(False))
self.process.finished.connect(lambda: self.runButton.setEnabled(True))
#Function Main Start
def main():
app =QApplication(sys.argv)
ui=gui()
ui.show()
sys.exit(app.exec_())
#Function Main END
if __name__ == '__main__':
main()
我想在使用 PyQt5
的 gui 中 实时 重定向 python 程序的控制台输出我的意思是,在 python 程序执行的每个输出中,gui 会立即显示此输出
我使用的代码在完成整个程序后只显示日志,并没有解决实时问题
你能帮帮我吗
在我的代码下方:
from PyQt5 import QtGui,QtCore
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap
class gui(QMainWindow):
def __init__(self):
super(gui, self).__init__()
self.initUI()
def dataReady(self):
cursor = self.output.textCursor()
cursor.movePosition(cursor.End)
cursor.insertText(str(self.process.readAll()))
self.output.ensureCursorVisible()
def callProgram(self):
# run the process
# `start` takes the exec and a list of arguments
self.process.start('python',['Robot.py'])
def initUI(self):
# Layout are better for placing widgets
layout = QHBoxLayout()
self.runButton = QPushButton('Run')
self.runButton.clicked.connect(self.callProgram)
self.output = QTextEdit()
self.setGeometry(100, 60, 1000, 800)
layout.addWidget(self.output)
layout.addWidget(self.runButton)
centralWidget = QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
# QProcess object for external app
self.process = QtCore.QProcess(self)
# QProcess emits `readyRead` when there is data to be read
self.process.readyRead.connect(self.dataReady)
# Just to prevent accidentally running multiple times
# Disable the button when process starts, and enable it when it finishes
self.process.started.connect(lambda: self.runButton.setEnabled(False))
self.process.finished.connect(lambda: self.runButton.setEnabled(True))
#Function Main Start
def main():
app =QApplication(sys.argv)
ui=gui()
ui.show()
sys.exit(app.exec_())
#Function Main END
if __name__ == '__main__':
main() ```
当您将 python 输出重定向到文件或管道时,出于性能原因,它会被缓冲。您需要使用 -u
cli 参数 或 编写 运行 脚本,像这样刷新每个打印输出:print(something, flush=True)
。有关详细信息,请参阅 this question。
self.process.start('python',['-u','Robot.py'])
完整的解决方案,感谢@mugiseybrows 和@eyllanesc
它解决了两个问题:第一个是在 gui 中重定向 python 程序的输出,第二个是在独立的行中显示每个输出
import sys
from PyQt5 import QtGui,QtCore
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap
class gui(QMainWindow):
def __init__(self):
super(gui, self).__init__()
self.initUI()
def dataReady(self):
cursor = self.output.textCursor()
cursor.movePosition(cursor.End)
#cursor.insertText(str(self.process.readAll()))
cursor.insertText(self.process.readAll().data().decode())
self.output.ensureCursorVisible()
def callProgram(self):
# run the process
# `start` takes the exec and a list of arguments
self.process.start('python',['-u','Robot.py'])
def initUI(self):
# Layout are better for placing widgets
layout = QHBoxLayout()
self.runButton = QPushButton('Run')
self.runButton.clicked.connect(self.callProgram)
self.output = QTextEdit()
self.setGeometry(100, 60, 1000, 800)
layout.addWidget(self.output)
layout.addWidget(self.runButton)
centralWidget = QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
# QProcess object for external app
self.process = QtCore.QProcess(self)
# QProcess emits `readyRead` when there is data to be read
self.process.readyRead.connect(self.dataReady)
# Just to prevent accidentally running multiple times
# Disable the button when process starts, and enable it when it finishes
self.process.started.connect(lambda: self.runButton.setEnabled(False))
self.process.finished.connect(lambda: self.runButton.setEnabled(True))
#Function Main Start
def main():
app =QApplication(sys.argv)
ui=gui()
ui.show()
sys.exit(app.exec_())
#Function Main END
if __name__ == '__main__':
main()