PyQt5 - TypeError: signal has 0 argument(s) but 1 provided

独自空忆成欢 提交于 2020-01-30 03:28:09

问题


I am trying to redirect "sys.stdout" to QTextEdit, here is my code:

class Communicate(QObject):  
    printText = pyqtSignal()
    def write(self, text):
        self.printText.emit(str(text))

class UI(QWidget):
    def __init__(self, parent = None):
        QWidget.__init__(self)
        ...
        self.textedit = QTextEdit(self)
        self.textedit.setGeometry(400,20,220,300)
        self.c = Communicate()
        self.c.printText.connect(self.textedit.insertPlainText)
        sys.stdout = self.c


if __name__ == "__main__":
    ...

When I ran the code, I got TypeError: Communicate.printText[] signal has 0 argument(s) but 1 provided. But when I tried to provide no argument to self.printText.emit(), it said that self.textedit.insertPlainText needs 1 argument. Did I miss something? Your answer will be appreciated.


回答1:


You need to specify the parameters when defining the signal. Also, you should probably provide a dummy flush method, to avoid attribute errors:

class Communicate(QObject):
    printText = pyqtSignal(str)

    def write(self, text):
        self.printText.emit(text)

    def flush(self):
        pass


来源:https://stackoverflow.com/questions/46693355/pyqt5-typeerror-signal-has-0-arguments-but-1-provided

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!