Understanding a piece of python code

My question relates to the accepted answer to the question How to capture Python interpreter output and show in a text widget? which shows how to redirect standard output to QTextEdit.

The author, Ferdinand Beyer, defines the EmittingStream class as such:

from PyQt4 import QtCore

class EmittingStream(QtCore.QObject):

    textWritten = QtCore.pyqtSignal(str)

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

It uses the class as follows:

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

I do not understand the line that creates an instance of the EmittingStream class. The keyword argument textWritten = self.normalOutputWritten seems to connect the textWritten-signal to the normalOutputWritten slot, but I don't understand why this works.

+4
source share
1 answer

:

, , pyqtConfigure() QObject. :

act = QtGui.QAction("Action", self)
act.triggered.connect(self.on_triggered)

act = QtGui.QAction("Action", self, triggered=self.on_triggered)

act = QtGui.QAction("Action", self)
act.pyqtConfigure(triggered=self.on_triggered)
+1

Source: https://habr.com/ru/post/1523529/


All Articles