Question

I am trying to write a code which allows me to present one questions per page (without opening new windows). I wanted to know how i could alter the code below to allow me to do this??

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_()
Was it helpful?

Solution

This should give you an idea of how to structure your program:

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_()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top