こちらの投稿を参考に。print ステートメントの出力を 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_())
コードが不完全であることはわかっています...どのシグナルを追加すればよいですか?