pyqt:qmessagebox重复

时间:2015-03-08 15:28:30

标签: python 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()

1 个答案:

答案 0 :(得分:2)

当您将第二个处理问题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)