Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从列表中获取要在GUI中显示的内容?_Python_List_User Interface_Pyqt4_Python 3.4 - Fatal编程技术网

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

Python 如何从列表中获取要在GUI中显示的内容?,python,list,user-interface,pyqt4,python-3.4,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.pus

我有一份清单:

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))

self.l
仅在调用
self.submit
时创建(它本身仅通过按钮的回调调用),因此如果在
submit
之前调用了
retranslateUi
,则
self.l
将不存在。您应该在对象的
\uuu init\uuu
方法中包含创建对象时预期存在的任何内容,即使该值只是一个占位符,例如
[]

什么是
l
??l是列表,因此l=[]在提供的答案中指出,变量在您创建它之前不存在,在您的例子中,
self.l
是在submit中创建的,因此很明显它会抛出错误。另一方面,
self.lineEdit.text==self.l()
是没有意义的,首先
self.lineEdit.text
是一个方法的名称,我认为您想要获取存储的文本,因此应该使用
self.lineEdit.text()
,另一个问题是self.l,正如您所指出的,它是一个列表,因此不能调用它()
将向您抛出另一个错误。因此,我应该将'self.l'变量放置在'retranslateUi'中,以便在'submit'按钮之前执行它?@DominicEaston,如果您计划在应用程序运行时再次调用
retranslateUi
(即通过上下文菜单更改语言),再次将
self.l
设置为空会踩下它以前的值,这可能不是用户期望的值。如果基本上整个应用程序在语言更改时都必须重新启动,这可能是合适的,但如果将它放在安装过程中会被调用的某个位置,例如
self.setupU,则更有意义我
或self.\uuuuuu初始化