如何从列表中获取要在GUI中显示的内容?

时间:2018-06-01 18:25:39

标签: python list user-interface pyqt4 python-3.4

我有一个清单:

def submit(self):
    self.l = []

我需要拥有GUI中列表显示中的内容

    def retranslateUi(self, Form):
    Form.setWindowTitle(_translate("Form", "Math Game", None))
    self.lineEdit.setStatusTip(_translate("Form", "Enter answer here", None))
    self.pushButton.setText(_translate("Form", "SUBMIT", None))
    self.label.setText(_translate("Form", self.l, None))

我收到错误:

        self.label.setText(_translate("Form", self.l, None))
AttributeError: 'Ui_Form' object has no attribute 'l'

我正在尝试创建一个儿童数学游戏(大学作业),列表中的任何内容都必须显示在GUI中,我做错了什么?我相信这可能与("Form, self.l, None")中的括号有关,但我不太确定。

这是整个代码:

    from PyQt4 import QtCore, QtGui
import random

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    #this is backend code
    def submit(self):
        self.l = []
        if self.lineEdit.text == self.l():
            print("Correct!")
        else:
            print("Incorrect!")
        #this is GUI code
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.setEnabled(True)
        Form.resize(311, 192)
        Form.setLayoutDirection(QtCore.Qt.LeftToRight)
        Form.setAutoFillBackground(True)
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(10, 140, 191, 41))
        self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Calibri"))
        font.setPointSize(20)
        font.setFamily(_fromUtf8("Calibri"))
        self.lineEdit.setFont(font)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(210, 140, 91, 41))
        self.pushButton.clicked.connect(self.submit)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Calibri"))
        font.setPointSize(12)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.widget = QtGui.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(10, 10, 291, 121))
        self.widget.setAutoFillBackground(True)
        p = self.widget.palette()
        p.setColor(self.widget.backgroundRole(), QtCore.Qt.white)
        self.widget.setPalette(p)
        self.widget.setObjectName(_fromUtf8("widget"))
        self.label = QtGui.QLabel(self.widget)
        self.label.setGeometry(QtCore.QRect(60, 40, 341, 41))
        font = QtGui.QFont()
        font.setPointSize(24)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setObjectName(_fromUtf8("label"))
        self.widget.raise_()
        self.lineEdit.raise_()
        self.pushButton.raise_()
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

        #translation of names from output name to edited name
    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Math Game", None))
        self.lineEdit.setStatusTip(_translate("Form", "Enter answer here", None))
        self.pushButton.setText(_translate("Form", "SUBMIT", None))
        self.label.setText(_translate("Form", self.l, None))

1 个答案:

答案 0 :(得分:1)

self.l仅在调用self.submit时创建(其本身仅通过按钮的回调调用),因此如果在retranslateUi之前调用submitself.l尚不存在。您应该在其__init__方法中包含对象创建中预期存在的任何内容,即使该值只是[]等占位符。