【问题标题】:PYQT: How to capture output of python's interpreter and display it in QEditText?PYQT:如何捕获 python 解释器的输出并将其显示在 QEditText 中?
【发布时间】:2013-03-26 13:03:12
【问题描述】:

参考帖子here。有人可以详细解释一下如何将打印语句的输出附加到 PYQT 中的 QEditext... 我尝试了上面给出的代码,但它不完整,我得到了:

TypeError: connect() slot argument should be a callable or a signal, not 'QTextEdit'

在我写的第一个文件中:

from PyQt4 import QtCore

class EmittingStream(QtCore.QObject):
    textWritten = QtCore.pyqtSignal(str)

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

在一个单独的文件中,我导入了第一个文件,它是这样的:

from PyQt4 import QtGui, QtCore

import os, sys

class Window(QtGui.QWidget):   
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.et=QtGui.QTextEdit()

        layout = QtGui.QVBoxLayout(self)            
        layout.addWidget(self.et)

        sys.stdout = EmittingStream(textWritten=self.et)        

    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.et.textCursor()
        cursor.movePosition(QtGui.QTextCursor.End)
        cursor.insertText(text)
        self.et.setTextCursor(cursor)
        self.et.ensureCursorVisible()

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

我知道我的代码不完整...我应该添加什么信号?

【问题讨论】:

    标签: python qt pyqt pyqt4 pyside


    【解决方案1】:

    看看这是否适合你:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4 import QtGui, QtCore
    
    class MyStream(QtCore.QObject):
        message = QtCore.pyqtSignal(str)
        def __init__(self, parent=None):
            super(MyStream, self).__init__(parent)
    
        def write(self, message):
            self.message.emit(str(message))
    
    class MyWindow(QtGui.QWidget):
        def __init__(self, parent=None):
            super(MyWindow, self).__init__(parent)
    
            self.pushButtonPrint = QtGui.QPushButton(self)
            self.pushButtonPrint.setText("Click Me!")
            self.pushButtonPrint.clicked.connect(self.on_pushButtonPrint_clicked)
    
            self.textEdit = QtGui.QTextEdit(self)
    
            self.layoutVertical = QtGui.QVBoxLayout(self)
            self.layoutVertical.addWidget(self.pushButtonPrint)
            self.layoutVertical.addWidget(self.textEdit)
    
        @QtCore.pyqtSlot()
        def on_pushButtonPrint_clicked(self):
            print "Button Clicked!"
    
        @QtCore.pyqtSlot(str)
        def on_myStream_message(self, message):
            self.textEdit.moveCursor(QtGui.QTextCursor.End)
            self.textEdit.insertPlainText(message)
    
    if __name__ == "__main__":
        import sys
    
        app = QtGui.QApplication(sys.argv)
        app.setApplicationName('MyWindow')
    
        main = MyWindow()
        main.show()
    
        myStream = MyStream()
        myStream.message.connect(main.on_myStream_message)
    
        sys.stdout = myStream        
        sys.exit(app.exec_())
    

    【讨论】:

    • 是的,代码工作正常,无论打印“xxx”如何打印在 PYQT 窗口上。作为下一步,我想在这个 inon_pushButtonPrint_clicked 函数中调用我的 python 文件,并希望我的文件的输出是在 PYQT 窗口中打印(即所有打印语句)
    • 这不是 Stackoverflow 的工作方式。请阅读about 页面并返回,完成后我很乐意为您提供帮助。
    • 要在QTextWidget 中显示文件的内容,如果您只使用with open("/path/to/file", "r") as fileInput: self.textEdit.setPlainText(fileInput.read()) 会更容易,如果您仍想打印内容,请使用with open("/path/to/file", "r") as fileInput: print fileInput.read()
    猜你喜欢
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多