0

ユーザーが回答を入力したときに確認メッセージ ボックスを追加しました。ただし、ユーザーが確認して次の質問に進むと、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()
4

1 に答える 1

2

質問 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)

1 つのシグナルに複数のスロットを接続できます。

これを変更するには、次の信号を接続する前に前の信号を明示的に切断します。例えば:

self.next_question.clicked.disconnect(self.HandleQuestion1)
于 2015-03-08T23:35:14.657 に答える