【问题标题】:TypeError while using QTextEdit methods使用 QTextEdit 方法时出现 TypeError
【发布时间】:2014-10-21 19:08:36
【问题描述】:

我已经使用 PyQt 构建了一个文本编辑器,我正在尝试向它显示一个字符串 我尝试使用QTextEdit.append()、QTextEdit.setText(),但在每种情况下都会出现 TypeError。

我在做:

qstring_result = QString(string_result)
QTextEdit.setText(qstring_result)

我收到一个错误:

TypeError: QTextEdit.append(QString): first argument of unbound method must have type 'QTextEdit'

我搜索了很多,但没有找到转换 String type to QTextEdit type 的方法。有什么办法解决这个问题?

代码:

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt

class Main(QtGui.QMainWindow):

    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self,parent)

        self.initUI()

    def initUI(self):

        self.setGeometry(100,100,1030,800)
        string_result = "This is correct output"
        qstring_result = QString(string_result)
        QTextEdit.setText(qstring_result)
        self.setWindowTitle("Writer")


def main():

    app = QtGui.QApplication(sys.argv)

    main = Main()
    main.show()

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

【问题讨论】:

  • @matsjoyce 我是 python 新手,你能告诉我怎么做吗?
  • 你能告诉我们更多代码吗?在某个地方你应该有类似something = QTextEdit() 的东西。你需要做something.setTest(qstring_result)。
  • @matsjoyce 我已经编辑了这个问题。它现在包含我的代码。
  • 该代码是由工具生成的,还是您从头开始编写的?
  • @matsjoyce 我正在学习 python GUI。从朋友的笔记本上学习。他从某个网站上写了所有东西..

标签: python string user-interface qstring qtextedit


【解决方案1】:

问题是类的方法(函数)必须用类的实例来调用:

>>> QTextEdit # class
<class 'PyQt4.QtGui.QTextEdit'>
>>> QTextEdit() # instance
<PyQt4.QtGui.QTextEdit object at 0x7f00ea379a68>
>>> type(QTextEdit)
<class 'PyQt4.QtCore.pyqtWrapperType'>
>>> type(QTextEdit())
<class 'PyQt4.QtGui.QTextEdit'>

错误消息显示该方法是“未绑定”的,这意味着它没有附加实例:

>>> QTextEdit.setText # unbound
<built-in function setText>
>>> QTextEdit().setText # bound
<built-in method setText of QTextEdit object at 0x7f00ea379a68>

因此,您必须在 QTextEdit 的实例上调用您的 setText 方法。您可以通过调用它来创建QTextEdit 的实例。您必须使用self 调用QTextEdit 的构造函数,以向QTextEdit 显示master 是什么(它所在的窗口)。到目前为止,您的程序应该是:

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt

class Main(QtGui.QMainWindow):

    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self,parent)
        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 500, 500)
        string_result = "This is correct output"
        text_edit = QtGui.QTextEdit(self)

然后您应该调用setText 方法。你应该做text_edit.setText,而不是QTextEdit.setText,因为text_edit 是你的实例。因此,您的下一行应该是:

        text_edit.setText(string_result)

然后你就可以完成你的程序了:

        text_edit.setGeometry(0, 0, 500, 500) # this just sets the size to 500 x 500
        self.setWindowTitle("Writer")


def main():
    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

然后您的程序会产生以下输出:

此外,由于您的程序是 zetcode 教程的一般形式,因此可能值得一试:http://zetcode.com/gui/pyqt4/

【讨论】:

    猜你喜欢
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    相关资源
    最近更新 更多