我正在尝试编写一个代码,允许我每页显示一个问题(不打开新窗口)。我想知道如何改变下面的代码以允许我这样做?
import sys
from PyQt4 import QtCore, QtGui
class StartTest(QtGui.QMainWindow):
def __init__(self, parent=None):
super(StartTest, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
QPage = Question(self)
self.central_widget.addWidget(QPage)
self.central_widget.setCurrentWidget(QPage)
class Question(QtGui.QWidget):
def __init__(self, label, parent=None):
super(Question, self).__init__(parent)
label = question1
question = QtGui.QLabel(label)
self.proceed = QtGui.QPushButton('Proceed')
self.Answer = QtGui.QLineEdit(self)
layout = QtGui.QFormLayout()
layout.addRow(question, self.Answer)
layout2 = QtGui.QVBoxLayout()
layout2.addLayout(layout)
layout2.addWidget(self.proceed)
self.setLayout(layout2)
self.proceed.clicked.connect(self.nextQ)
def nextQ(self):
Answer = self.Answer.text()
studentAns.append(Answer)
label = question2
studentAns = []
question1 = 'What is 5+5?'
question2 = 'What is 45+10?'
if __name__ == '__main__':
User = ''
app = QtGui.QApplication([])
window = StartTest()
window.showFullScreen()
app.exec_()
答案 0 :(得分:3)
这可以让您了解如何构建程序:
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.showFullScreen()
app.exec_()