0

I've added a confirmation message box when the user types in the answer. However, when the user confirms and proceeds to the next question, 2 message boxes are showing up, then 3 and so on. How can I fix this duplication error?

class IntegrationQuestions(QtGui.QMainWindow):
    def __init__(self, parent = None):                
        QtGui.QDialog.__init__(self, parent)
        self.setWindowTitle('Integration')
        self.setMinimumSize(265,400)
        self.setMaximumSize(266,401)
        self.Question1()

    def Question1(self):
        #gets questions from another file
        from FQuestions import FIntQuestion, FIntAnswer
        self.lbl1 = QtGui.QLabel("Integrate the equation below",self)
        self.lbl1.move(0,0)
        self.lbl1.resize(200,20)

        self.lbl2 = QtGui.QLabel(pretty(FIntQuestion[0], use_unicode = False), self)
        self.lbl2.resize(200, 80)
        self.lbl2.move(30,30)

        self.lbl3 = QtGui.QLabel("Sketch pad",self)

        self.lbl3.move(0,120)
        #free area the user can use for working out
        self.SketchPad = QtGui.QTextEdit(self)
        self.SketchPad.resize(250,150)
        self.SketchPad.move(0,150)

        self.lbl4 = QtGui.QLabel("Answer",self)
        self.lbl4.move(0,300)
        #the answer the user types in 
        self.Answer = QtGui.QLineEdit(self)
        self.Answer.move(0,330)
        self.Answer.resize(250,20)

        self.next_question = QPushButton('Next', self)
        self.next_question.move(160,360)

        self.next_question.clicked.connect(self.HandleQuestion1)
        #this is the score the user gets after answering all questions
        self.score = 0
        #for testing 
        print(FIntAnswer[0])

    def HandleQuestion1(self):
        from FQuestions import FIntAnswer
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure this is your final answer?", QtGui.QMessageBox.Yes | 
            QtGui.QMessageBox.No, QtGui.QMessageBox.Yes)
        #if the answer the user types in is the same as the correct answer 1 should be added to the score
        if reply == QtGui.QMessageBox.Yes:
            if self.Answer.text() == FIntAnswer[0]:
                self.score = self.score + 1
            else:
                self.score = self.score + 0
            #connect to the next question if the user clicks yes
            self.Question2()

    def Question2(self):
        #for testing
        print(self.score)
        from FQuestions import FIntQuestion, FIntAnswer
        self.lbl2.setText(pretty(FIntQuestion[1], use_unicode = False))
        self.next_question.clicked.connect(self.HandleQuestion2)
        #for testing
        print(FIntAnswer[1])

    #this is where the duplication error is occurring     
    def HandleQuestion2(self):
        from FQuestions import FIntAnswer
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure this is your final answer?", QtGui.QMessageBox.Yes | 
            QtGui.QMessageBox.No, QtGui.QMessageBox.Yes)

        if reply == QtGui.QMessageBox.Yes:
            if self.Answer.text() == FIntAnswer[1]:
                self.score = self.score + 1
            else:
                self.score = self.score + 0
            self.Question3()
mcseth antwi
  • 131
  • 1
  • 1
  • 9

1 Answers1

2

When you connect the second method for handling question 2 to the next_question button, this does not disconnect the first handler.

# Connects HandleQuestion1 method
self.next_question.clicked.connect(self.HandleQuestion1)
# Connects HandleQuestion2 method in addition to any existing connections
self.next_question.clicked.connect(self.HandleQuestion2)

You can have multiple slots connected to one signal!

To change this, you can explicitly disconnected previous signals before connecting the next one. For example:

self.next_question.clicked.disconnect(self.HandleQuestion1)
three_pineapples
  • 11,579
  • 5
  • 38
  • 75