如何捕获 Python 解释器的输出并显示在文本小部件中? [英] How to capture output of Python's interpreter and show in a Text widget?

查看:60
本文介绍了如何捕获 Python 解释器的输出并显示在文本小部件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 PyQt 的 Python 程序,旨在在 Windows 上运行.该程序进行了大量操作并打印了大量信息.但是因为我想冻结它并且不希望出现提示屏幕,所以我希望所有信息都出现在主应用程序中,在 QTextEdit 左右.我怎样才能让程序工作,以便它从解释器获取输出并同时在 textEdit 上显示它,就像它在真正的解释器上所做的那样?

I have a program in Python with PyQt, designed to run on Windows. This program makes a lot of operations and prints a lot of info. But as I want to freeze it and don't want the prompt screen to appear, I want that all that info appears in the main application, in a QTextEdit or so. How can i make the program work so it gets the output from the interpreter and shows it on the textEdit at the same time, just like it does on the real interpreter?

推荐答案

我假设解释器的输出"是指写入控制台或终端窗口的输出,例如使用 print() 生成的输出.

I assume that with "output from the interpreter", you mean output written to the console or terminal window, such as output produced with print().

Python 产生的所有控制台输出都被写入程序的输出流 sys.stdout(正常输出)和 sys.stderr(错误输出,例如异常回溯).这些是类似文件的对象.

All console output produced by Python gets written to the program's output streams sys.stdout (normal output) and sys.stderr (error output, such as exception tracebacks). These are file-like objects.

您可以用您自己的类文件对象替换这些流.您的自定义实现必须提供一个 write(text) 函数.通过提供您自己的实现,您可以将所有输出转发到您的小部件:

You can replace these streams with your own file-like object. All your custom implementation must provide is a write(text) function. By providing your own implementation, you can forward all output to your widget:

class MyStream(object):
    def write(self, text):
        # Add text to a QTextEdit...

sys.stdout = MyStream()
sys.stderr = MyStream()

如果您需要重置这些流,它们仍然可以作为 sys.__stdout__sys.__stderr__ 使用:

If you ever need to reset these streams, they are still available as sys.__stdout__ and sys.__stderr__:

sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

<小时>

更新

这是 PyQt4 的一些工作代码.首先定义一个流,用 Qt 信号报告写入它的数据:

Here is some working code for PyQt4. First define a stream that reports data written to it with a Qt signal:

from PyQt4 import QtCore

class EmittingStream(QtCore.QObject):

    textWritten = QtCore.pyqtSignal(str)

    def write(self, text):
        self.textWritten.emit(str(text))

现在,在您的 GUI 中,将此流的实例安装到 sys.stdout 并将 textWritten 信号连接到将文本写入 的插槽QTextEdit:

Now, in your GUI, install an instance of this stream to sys.stdout and connect the textWritten signal to a slot that writes the text to a QTextEdit:

# Within your main window class...

def __init__(self, parent=None, **kwargs):
    # ...

    # Install the custom output stream
    sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)

def __del__(self):
    # Restore sys.stdout
    sys.stdout = sys.__stdout__

def normalOutputWritten(self, text):
    """Append text to the QTextEdit."""
    # Maybe QTextEdit.append() works as well, but this is how I do it:
    cursor = self.textEdit.textCursor()
    cursor.movePosition(QtGui.QTextCursor.End)
    cursor.insertText(text)
    self.textEdit.setTextCursor(cursor)
    self.textEdit.ensureCursorVisible()

这篇关于如何捕获 Python 解释器的输出并显示在文本小部件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆