简体   繁体   中英

Two questions about direct console to Pyqt GUI

I want to direct console to Pyqt GUI

And I Search stackoverflow, find a answer

Print out python console output to Qtextedit

the code show below

class Stream(QtCore.QObject):
    newText = QtCore.pyqtSignal(str)

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

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        self.home()

        sys.stdout = Stream(newText=self.onUpdateText)

    def onUpdateText(self, text):
        cursor = self.process.textCursor()
        cursor.movePosition(QtGui.QTextCursor.End)
        cursor.insertText(text)
        self.process.setTextCursor(cursor)
        self.process.ensureCursorVisible()

    def __del__(self):
        sys.stdout = sys.__stdout__

i have two questions.

  1. why def write(self, text) is defined but not use

  2. What does the parameter in Stream(newText=self.onUpdateText) mean, and my pycharm give me a warn Unexpected argument

1. why def write(self, text) is defined but not use

To understand why the write method is implemented, just read the docs of the print built-in:

Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead .

Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

Changed in version 3.3: Added the flush keyword argument.

(emphasis mine)

As indicated, the "print" function implements a logic that can write the text in simple words (adding the sep, end, etc) in a file that by default is sys.stdout through the write method.

So the objective is not to write on the sys.stdout device but to redirect the text, so that method must be modified so that it sends the information through the newText signal.

2. What does the parameter in Stream(newText=self.onUpdateText) mean, and my pycharm give me a warn Unexpected argument.

By default the QObjects can receive the kwargs to the initial values of the qproperties and make the connections of the qsignals. In this case it is the second option, so

sys.stdout = Stream(newText=self.onUpdateText)

equals

sys.stdout = Stream()
sys.stdout.newText.connect(self.onUpdateText)

Pycharm indicates the warning "unexpected argument" because the logic it indicates is implemented in C++ (via SIP) and the IDE is not able to handle them. Just skip it as it is only a limitation of the IDE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM