将列表长度放入 PyQt4 中的 QLabel
Putting list length to QLabel in PyQt4
我在 PyQt4 中制作了多页测验应用程序,我试图在最后添加分数摘要,因此它会计算正确和错误答案的数量。
为此我列出了清单,我是这样制作的:
def scorecheck(self, sendercheck): # sendercheck object gets from which widget was signal sent.
wronganswers = []
correctanswers = []
if sendercheck == ( self.answ or self.answ1 ):
wronganswers.append(1)
if sendercheck == ( self.answ2 ):
correctanswers.append(1)
如何在 QLabel 中输入 wronganswers
长度?
我试过这些:
self.wronganswerlabel = QtGui.QLabel(self)
self.wronganswerlabel.setText(len(wronganswers))
self.wronganswerlabel.setGeometry(200, 200, 200, 200)
self.wronganswerlabel.show()
给我这个错误:
self.wronganswer.setText(len(wronganswers))
TypeError: QLabel.setText(QString): argument 1 has unexpected type 'int'
和这个:
self.wronganswerlabel = QtGui.QLabel(self, "Score:".len(wronganswers))
self.wronganswerlabel.setGeometry(200, 200, 200, 200)
self.wronganswerlabel.show()
给我报错:
self.wronganswer = QtGui.QLabel(self, "Score:".len(wronganswers))
AttributeError: 'str' object has no attribute 'len'
这只是一个铸造的东西; QtGui.QLabel()
and QtGui.QLabel.setText()
需要一个 QtCore.QString
或只是一个 unicode
或 string
对象,但您正试图传递一个 int。您需要告诉 python 如何将整数转换为字符串。通常的方式是str(myint)
此外,您需要使用 + 运算符来组合两个字符串——按照您现在的语法,您正在询问字符串 "Score:"
的长度方法,这可能不是您的意思.
以下应该按预期工作
self.wronganswerlabel.setText(str(len(wronganswers)))
或
self.wronganswerlabel = QtGui.QLabel("Score:" + str(len(wronganswers)), self)
我在 PyQt4 中制作了多页测验应用程序,我试图在最后添加分数摘要,因此它会计算正确和错误答案的数量。
为此我列出了清单,我是这样制作的:
def scorecheck(self, sendercheck): # sendercheck object gets from which widget was signal sent.
wronganswers = []
correctanswers = []
if sendercheck == ( self.answ or self.answ1 ):
wronganswers.append(1)
if sendercheck == ( self.answ2 ):
correctanswers.append(1)
如何在 QLabel 中输入 wronganswers
长度?
我试过这些:
self.wronganswerlabel = QtGui.QLabel(self)
self.wronganswerlabel.setText(len(wronganswers))
self.wronganswerlabel.setGeometry(200, 200, 200, 200)
self.wronganswerlabel.show()
给我这个错误:
self.wronganswer.setText(len(wronganswers))
TypeError: QLabel.setText(QString): argument 1 has unexpected type 'int'
和这个:
self.wronganswerlabel = QtGui.QLabel(self, "Score:".len(wronganswers))
self.wronganswerlabel.setGeometry(200, 200, 200, 200)
self.wronganswerlabel.show()
给我报错:
self.wronganswer = QtGui.QLabel(self, "Score:".len(wronganswers))
AttributeError: 'str' object has no attribute 'len'
这只是一个铸造的东西; QtGui.QLabel()
and QtGui.QLabel.setText()
需要一个 QtCore.QString
或只是一个 unicode
或 string
对象,但您正试图传递一个 int。您需要告诉 python 如何将整数转换为字符串。通常的方式是str(myint)
此外,您需要使用 + 运算符来组合两个字符串——按照您现在的语法,您正在询问字符串 "Score:"
的长度方法,这可能不是您的意思.
以下应该按预期工作
self.wronganswerlabel.setText(str(len(wronganswers)))
或
self.wronganswerlabel = QtGui.QLabel("Score:" + str(len(wronganswers)), self)