我有以下代码,但想知道我如何从'Question x'
开始,其中x
是用户输入的随机整数。即,我将如何从2nd question
或3rd question
而不是question 1
开始。此外,如果我从说question 2
开始,则question 1
根本不应该显示。同样,如果我从question 3
开始,那么question 1
和2
根本不应被包含/显示(无论最后一个位置如何)。
代码
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_()
答案 0 :(得分:2)
window = StartTest(questions[start_num:])
您将start_num
定义为起始问题,索引从0开始。因此,问题1将为start_num
0。