pyqt:qmessagebox复制

2024-10-03 04:27:12 发布

您现在位置:Python中文网/ 问答频道 /正文

当用户输入答案时,我添加了一个确认消息框。但是,当用户确认并继续下一个问题时,将显示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()

Tags: theanswerfromselfmoveifisdef
1条回答
网友
1楼 · 发布于 2024-10-03 04:27:12

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

相关问题 更多 >