如何随机化pyqt中单选按钮的顺序 [英] How to randomize the order of radio buttons in pyqt

查看:81
本文介绍了如何随机化pyqt中单选按钮的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个测验,我希望单选按钮位于不同的位置.我让它在一定程度上工作,因为它在第一个问题中是随机顺序的,但是它会在剩下的测验中保持这个顺序.我希望它每次都随机化.

im making a quiz and i want the radiobuttons to be in different positions. ive got it working to a extent as it would be in random order in the first question however it would then stay in that order for the rest of the quiz. I want it to randomize every time.

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) 

推荐答案

如果您将答案选项加载到容器中,您可以使用 Python 的 random (link) 使用以下一种或两种工具的库:

If you load your answer choices into a container, you can use Python's random (link) library with one or both of the following tools:

  • random.choice(seq) (link) 随机选择 seq
  • 中提供的可能项目
  • random.sample(pop, k) (link) 从 pop 中提供的可能选择中选择 k 个唯一元素,不替换,我认为这是关键在您的情况下,因为您不希望在同一问题上两次显示相同的答案.
  • random.choice(seq) (link) which makes a random choice of the possible items provided in seq
  • random.sample(pop, k) (link) which choice k unique elements from the possible choices provided in pop, without replacing, which I think is key in your situation because you don't want to have the same answer displayed twice on the same question.

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

This would simplify your current if-statement block, from

if randomnumber == 0:
    layout.addWidget(self.Correctanswer)
    layout.addWidget(self.Incorrectans1)
    layout.addWidget(self.Incorrectans2)
    layout.addWidget(self.Incorrectans3)

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

还刚刚想到了另一个很酷的主意.如果您有大量可能的错误答案池",您可以专门定义一个函数来提供随机选择的错误答案.类似的东西:

Also just thought of another cool idea. If you have a large "pool" of possible incorrect answers, you can define a function specifically to provide a random selection of wrong answers. Something like:

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

这将为您提供 number 错误的答案以添加到布局中以及正确的答案.

which would provide you with number incorrect answers to add to the layout along with the correct.

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)(link) 是另一种方法,因为它需要序列 x 并将它们混合在一起.所以你可以结合 random.sample(wrongChoices, 3) 的想法,给你三个随机的错误答案,然后添加正确的答案并确保正确的答案并不总是在底部,用 random.shuffle(allAnswerChoices)

Finally, random.shuffle(x) (link) be another method, as it takes the sequence x and mixes them up in place. So you could combine the random.sample(wrongChoices, 3) idea which gives you three random incorrect answers, then add in the correct answer and to make sure the correct answer isn't always at the bottom, give em a final stir with random.shuffle(allAnswerChoices)

这篇关于如何随机化pyqt中单选按钮的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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