samedi 28 mars 2015

How to randomize the order of radio buttons in 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)


The form is dyanamic so it changes the labels everytime the next button is pressed but im thinking that may be having a influence



def Showquestions2(self):
self.Questionum.setText("Question 2")
self.Questioninfo.setText(self.Questions[0][1])
self.Correctanswer.setText(self.Questions[0][2])
self.Incorrectans1.setText(self.Questions[0][5])
self.Incorrectans2.setText(self.Questions[0][6])
self.Incorrectans3.setText(self.Questions[0][7])
self.ismultichoiceButton.setText("Next question")
self.ismultichoiceButton.clicked.connect(self.Showquestions3)


is there anyway i can fix this? Thanks,





Aucun commentaire:

Enregistrer un commentaire