pyqt: дублирование qmessagebox

Я добавил окно подтверждения, когда пользователь вводит ответ. Однако, когда пользователь подтверждает и переходит к следующему вопросу, появляются 2 окна сообщений, затем 3 и так далее. Как я могу исправить эту ошибку дублирования?

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()

person mcseth antwi    schedule 08.03.2015    source источник


Ответы (1)


Когда вы подключаете второй метод обработки вопроса 2 к кнопке next_question, это не отключает первый обработчик.

# 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)

К одному сигналу можно подключить несколько слотов!

Чтобы изменить это, вы можете явно отключить предыдущие сигналы перед подключением следующего. Например:

self.next_question.clicked.disconnect(self.HandleQuestion1)
person three_pineapples    schedule 08.03.2015
comment
Большое спасибо, это помогло - person mcseth antwi; 09.03.2015