我如何从列表中的点x枚举?

时间:2014-05-03 17:00:45

标签: python pyqt pyqt4 python-3.3

我有以下代码,但想知道我如何从'Question x'开始,其中x是用户输入的随机整数。即,我将如何从2nd question3rd question而不是question 1开始。此外,如果我从说question 2开始,则question 1根本不应该显示。同样,如果我从question 3开始,那么question 12根本不应被包含/显示(无论最后一个位置如何)。

代码

import sys
from PyQt4 import QtCore, QtGui

class StartTest(QtGui.QMainWindow):
    def __init__(self, questions, parent=None):
        super(StartTest, self).__init__(parent)
        self.stack = QtGui.QStackedWidget(self)
        self.setCentralWidget(self.stack)
        for index, question in enumerate(questions):
            page = Question(question, self)
            page.submit.clicked[()].connect(
                lambda index=index: self.handleSubmit(index))
            self.stack.addWidget(page)
        self.answers = []

    def handleSubmit(self, index):
        page = self.stack.widget(index)
        answer = page.answer.text()
        # validate submitted answer...
        self.answers.append(answer)
        if index < self.stack.count() - 1:
            self.stack.setCurrentIndex(index + 1)

class Question(QtGui.QWidget):
    def __init__(self, question, parent=None):
        super(Question, self).__init__(parent)
        self.question = QtGui.QLabel(question, self)
        self.answer = QtGui.QLineEdit(self)
        self.submit = QtGui.QPushButton('Submit', self)
        form = QtGui.QFormLayout()
        form.addRow(self.question, self.answer)
        layout = QtGui.QVBoxLayout(self)
        layout.addLayout(form)
        layout.addWidget(self.submit)

if __name__ == '__main__':
    User = ''
    app = QtGui.QApplication([])
    questions = [
        'What is 5+5?',
        'What is 45+10?',
        'What is 28+47?',
        'What is 22+13?',
        ]
    window = StartTest(questions)
    window.show()
    app.exec_()

1 个答案:

答案 0 :(得分:2)

window = StartTest(questions[start_num:])

您将start_num定义为起始问题,索引从0开始。因此,问题1将为start_num 0。