Understanding a piece of python code

jan

My question refers to the accepted answer of the question How to capture output of Python's interpreter and show in a Text widget? which shows how to redirect standard output to a QTextEdit.

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

from PyQt4 import QtCore

class EmittingStream(QtCore.QObject):

    textWritten = QtCore.pyqtSignal(str)

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

He uses the class like this:

# 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 don't understand the line that instantiates the EmittingStream class. It looks as if the keyword argument textWritten=self.normalOutputWritten connects the textWritten-signal to the normalOutputWritten-slot, but I do not understand why this works.

unutbu

This feature is documented here:

It is also possible to connect signals by passing a slot as a keyword argument corresponding to the name of the signal when creating an object, or using the pyqtConfigure() method of QObject. For example the following three fragments are equivalent:

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Matlab: Understanding a piece of code

From Dev

Understanding Piece of C# code

From Dev

Understanding Piece of C# code

From Dev

Is there a Python function for this piece of code?

From Dev

Is there a Python function for this piece of code?

From Dev

Understanding a piece of code from the OpenGL Red Book

From Dev

What is this piece of code doing, python

From Dev

Convert JavaScript piece of code to python

From Dev

A simple way to run a piece of python code in parallel?

From Dev

Python: Pressing enter calls a piece of code or a function

From Dev

Understanding this python code better

From Dev

Code understanding python

From Dev

Understanding the details of this Python Code

From Java

Python: Writing quite complicated piece of code as a List Comprehension

From Java

How can i repeat a piece of code in python 3.8

From Dev

What is the difference between the following piece of code declaration in python

From Dev

Python beginner, understanding some code

From Dev

understanding this multithreading demon python code

From Dev

python with addition and multiplication not understanding the code

From Dev

Needs help for understanding for python code

From Dev

understanding this multithreading demon python code

From Dev

Understanding code for concatenating files in Python?

From Dev

Understanding Python Code Using Slicing

From Dev

Need help understanding Python Code

From Java

What are the parenthesis in this piece of code for?

From Dev

Why is this piece of code not faster?

From Dev

Explain a piece of Haskell code

From Dev

Refactoring this piece of code - Rails

From Dev

What is wrong with this piece of code?

Related Related

HotTag

Archive