如何随机化pyqt中单选按钮的顺序

时间:2015-03-28 17:16:00

标签: python random pyqt qradiobutton

我正在进行测验,我希望radiobuttons处于不同的位置。我已经在一定程度上工作了,因为它会在第一个问题中以随机顺序运行,但是它将在其余的测验中保持该顺序。我希望它每次都随机化。

class MultipleChoice(QtGui.QWidget):
showTopicsSignal = pyqtSignal()

def __init__(self,parent=None):
    super(MultipleChoice, self).__init__(parent)

    self.initUI()


def initUI(self):
    self.Questions=[]
    self.Questionum = QLabel()
    self.Questioninfo = QLabel()
    self.Correctanswer = QRadioButton()
    self.Incorrectans1 = QRadioButton()
    self.Incorrectans2 = QRadioButton()
    self.Incorrectans3 = QRadioButton()
    self.Correctanswer.setAutoExclusive(True)
    self.Incorrectans1.setAutoExclusive(True)
    self.Incorrectans2.setAutoExclusive(True)
    self.Incorrectans3.setAutoExclusive(True)
    layout = QVBoxLayout(self)
    layout.addWidget(self.Questionum)
    layout.addWidget(self.Questioninfo)
    randomnumber = randint(0,3)
    if randomnumber == 0:
        layout.addWidget(self.Correctanswer)
        layout.addWidget(self.Incorrectans1)
        layout.addWidget(self.Incorrectans2)
        layout.addWidget(self.Incorrectans3)
    elif randomnumber == 1:
        layout.addWidget(self.Incorrectans1)
        layout.addWidget(self.Correctanswer)
        layout.addWidget(self.Incorrectans2)
        layout.addWidget(self.Incorrectans3)
    elif randomnumber == 2:
        layout.addWidget(self.Incorrectans1)         
        layout.addWidget(self.Incorrectans2)
        layout.addWidget(self.Correctanswer)
        layout.addWidget(self.Incorrectans3)
    elif randomnumber == 3:
        layout.addWidget(self.Incorrectans1)         
        layout.addWidget(self.Incorrectans2)
        layout.addWidget(self.Incorrectans3)
        layout.addWidget(self.Correctanswer) 

2 个答案:

答案 0 :(得分:3)

如果您将答案选项加载到容器中,则可以使用Python的randomlink)库与以下一个或两个工具:

  • random.choice(seq)link)随机选择seq
  • 中提供的可能项目
  • random.sample(pop, k)linkk中提供的可能选项pop选择if randomnumber == 0: layout.addWidget(self.Correctanswer) layout.addWidget(self.Incorrectans1) layout.addWidget(self.Incorrectans2) layout.addWidget(self.Incorrectans3) 个唯一元素而不替换,我认为是因为你不希望在同一个问题上显示两次相同的答案,所以关键在于你的情况。

这将简化您当前的if语句块,来自

choices = [self.Correctanswer, self.Incorrectans1, self.Incorrectans2, self.Incorrectans3]
for q in range(4):
    layout.addWidget(random.sample(choices, 1))

def provideWrongAnswers(self, number):
    return random.sample(self.allWrongAnswersPool, number)

还想到了另一个很酷的主意。如果你有一个庞大的游泳池"可能的错误答案,您可以专门定义一个功能,以提供随机选择的错误答案。类似的东西:

number

这将为您提供In [7]: choices = range(1,11) In [9]: random.sample(choices, 3) # returns a different order of choices each time Out[9]: [1, 7, 3] In [10]: random.sample(choices, 3) Out[10]: [6, 9, 3] In [11]: random.sample(choices, 3) Out[11]: [5, 4, 2] In [12]: random.sample(choices, 3) Out[12]: [3, 6, 1] In [13]: random.sample(choices, 3) Out[13]: [10, 8, 3] In [14]: random.sample(choices, 3) Out[14]: [1, 7, 2] In [15]: random.sample(choices, 3) Out[15]: [9, 7, 3] 错误的答案,以及正确添加到布局。

random.shuffle(x)

最后,xlink)是另一种方法,因为它采用序列random.sample(wrongChoices, 3)并将它们混合到位。所以你可以结合random.shuffle(allAnswerChoices)这个给你三个随机错误答案的想法,然后加上正确的答案,并确保正确的答案不是总是在底部,给他们最后一点{{{ 1}}

答案 1 :(得分:0)

你当前的结构似乎过于复杂。将问题选择随机化而不是小部件本身会更简单。

这是一个简单的演示,展示了如何做到这一点:

import sys, random
from PyQt4 import QtGui

class MultipleChoice(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MultipleChoice, self).__init__(parent)
        self.Questions = [
            ['What is the capital of Bhutan?',
             'Thimphu', 'Bujumbura', 'Suva', 'Kigali'],
            ['What is the capital of Yemen?',
             'Sana\'a', 'Malabo', 'Riyadh', 'Muscat'],
            ['What is the capital of Togo?',
             'Lomé', 'Dakar', 'Abuja', 'Nouakchott'],
            ]
        self.Questionum = QtGui.QLabel(self)
        self.Questioninfo = QtGui.QLabel(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.Questionum)
        layout.addWidget(self.Questioninfo)
        self.buttonGroup = QtGui.QButtonGroup(self)
        self.buttonGroup.buttonClicked.connect(self.handleChoice)
        for index in range(4):
            button = QtGui.QRadioButton(self)
            self.buttonGroup.addButton(button, index)
            layout.addWidget(button)
        self.buttonNext = QtGui.QPushButton('Next', self)
        self.buttonNext.clicked.connect(self.handleNext)
        layout.addWidget(self.buttonNext)
        self.index = -1
        self.handleNext()

    def handleNext(self):
        self.index += 1
        if self.index >= len(self.Questions):
            self.index = 0
        question = self.Questions[self.index]
        self.Questionum.setText('Question %d:' % (self.index + 1))
        self.Questioninfo.setText(question[0])
        choices = question[1:]
        random.shuffle(choices)
        self.buttonGroup.setExclusive(False)
        for index, choice in enumerate(choices):
            button = self.buttonGroup.button(index)
            button.setChecked(False)
            button.setText(choice)
        self.buttonGroup.setExclusive(True)

    def handleChoice(self, button):
        answer = self.Questions[self.index][1]
        if button.text() == answer:
            print('Right!')
        else:
            print('Wrong...')

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = MultipleChoice()
    window.show()
    sys.exit(app.exec_())