2

With reference to the post here. Can someone give me a detailed explanation on how to append the print statement's output to the QEditext in PYQT... I tried the code given above but it was incomplete and I got:

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

In the first file i wrote:

from PyQt4 import QtCore

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

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

In a seperate file I imported the first file and it is like this:

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_())

I know my code is incomplete... What signal should I add?

Community
  • 1
  • 1
user2081918
  • 29
  • 1
  • 3

1 Answers1

4

See if this works for you:

#!/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_())
  • Ya the code works fine and whatever print "xxx" gets printed on the PYQT window.. As a next step I want to call my python file in this inon_pushButtonPrint_clicked function and want output of my file to be printed in PYQT window(i.e, all the print statements) – user2081918 Mar 29 '13 at 05:35
  • That is not the way Stackoverflow is intended to work. Please read the [about](http://stackoverflow.com/about) page and come back, I'll be glad to help you once your are done. –  Mar 29 '13 at 09:26
  • To display the contents of a file in your `QTextWidget` it would be easier if you just do `with open("/path/to/file", "r") as fileInput: self.textEdit.setPlainText(fileInput.read())`, if you still want to do it printing the contents, use `with open("/path/to/file", "r") as fileInput: print fileInput.read()` –  Mar 29 '13 at 15:53