如何每页显示一个 Test_Question (String)?(PyQt/Python) [英] How do I show a Test_Question (String) per page?? (PyQt/Python)

查看:25
本文介绍了如何每页显示一个 Test_Question (String)?(PyQt/Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 PyQt 应用程序,它为用户创建和设置测试.但是,在设置测试问题时,我希望每页查看 1 个问题.例如,如果用户要开始测试,则会出现一个问题.一旦他们回答了这个问题,然后他们点击一个按钮 'Proceed' ,它会显示下一页,只显示第二个问题等等......但是,就我而言,我不知道如何每个测试中有很多问题,因此页数会有所不同.我正在使用切换布局,因为我宁愿切换布局而不是打开新窗口,但这意味着我必须为每个问题创建一个类,并且因为每个测试的问题各不相同,我不确定我将如何提出问题????

I am creating a PyQt application which creates and sets tests for the user. However, when setting the test questions i wanted 1 question to be viewed per page. For example, if the user was to start a test, then a question would appear. Once they have answered that question, then they click a button 'Proceed' which presents the next page showing only the 2nd question and so on... However, in my case, i am unaware as to how many questions there are in each test, so the number of pages will vary. I am using switching layouts as i would rather switch layouts as opposed to open new windows but that means that i have to create a class per question and because questions per test vary, i am unsure as to how i will present the questions????

我不会复制我的整个应用程序代码,但会在下面提供一个基本示例:

I am not going to copy out my entire application code but will provide a basic example below:

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)
        question1 = Question1(self)
        self.central_widget.addWidget(question1)
        self.central_widget.setCurrentWidget(question1)
        question1.proceed.clicked.connect(self.question2)
    def question2(self):
        question2 = Question2(self)
        self.central_widget.addWidget(question2)
        self.central_widget.setCurrentWidget(question2)

class Question1(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Question1, self).__init__(parent)
        question = QtGui.QLabel('What is 5+5?')
        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)

class Question2(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Question2, self).__init__(parent)
        question = QtGui.QLabel('What is 45+10?')
        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)
#....
if __name__ == '__main__':
    User = ''
    app = QtGui.QApplication([])
    window = StartTest()
    window.showFullScreen()
    app.exec_()

推荐答案

只需一个可以参数化的 Question 类.对于问题中的示例代码,这就像将标签文本作为参数传递一样简单,因为这是唯一不同的地方:

Just have a single Question class that can be parameterized. For the example code in the question, this would be as simple as passing the label text as an argument, since this is the only thing that is different:

class Question(QtGui.QWidget):
    def __init__(self, label, parent=None):
        super(Question, self).__init__(parent)
        question = QtGui.QLabel(label)
        ...

question1 = Question('What is 5+5?', self)
question2 = Question('What is 45+10?', self)

如果某些问题类型具有不同的结构,您可以为每种类型创建子类:

If some question types have a different structure, you could create subclasses for each type:

class ComplexQuestion(Question):
    def __init__(self, label, parent=None):
        super(ComplexQuestion, self).__init__(parent)
        # do additional initialization...

或者,如果您想避免子类化,您可以简单地向 Question 类添加方法来启用/禁用某些功能.

Or if you wanted to avoid subclassing, you could simply add methods to the Question class that enabled/disabled certain features.

这篇关于如何每页显示一个 Test_Question (String)?(PyQt/Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆