Getting 1 QPushButton to perform 2 different function calls



  • Preface: I am working on a flashcard app in PyQt5 and I am trying to have an "Enter" button do an action that compares 2 strings by calling a function(one of which from the textEdit box, the other from a list of strings that I have pre coded.) Secondly, I want it to show the comparison as "Right or wrong" by calling the checkanswer() function, but when I press the button and try to get it to call a different function, it will repeat the same behavior from checkanswer().. even though I am trying to call nextcard() [in the print("correct") case]

    Which brings me to my question: Can each QPushButton do only one action? I want it to inform the user (or force them to enter the correct answer) before moving on to the next word. Essentially I want one button to call the checkanswer() function as well as the nextword() method at different times.

    Here is the code for my main window.. If you need any more information let me know and I will add it here.

    ```
    

    class MainWindow(QtWidgets.QMainWindow):

        textvalue = ""
        cardnum = 0                                                     # intialize card number from 0
        def __init__(self):
            super().__init__()
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.pushButtonEnter.clicked.connect(self.checkanswer)
            self.show()
    
        def checkanswer(self):
            textvalue = self.ui.lineEditAnswer.text()
            print("You entered: " + textvalue + " $? " + deck[self.cardnum].deflist[0])
            if textvalue == deck[self.cardnum].deflist[0]:
                print("Correct!")
                self.ui.lineEditAnswer.clear()
                textvalue = ""
                self.ui.labelVocab.setText("Correct!\n " + deck[self.cardnum].deflist[0])
                self.ui.pushButtonEnter.setText("Continue")
                self.ui.pushButtonEnter.clicked.connect(self.nextword)
            else:
                print("Incorrect!")
                print(self.cardnum)
                self.ui.lineEditAnswer.clear()
                self.ui.labelVocab.setText("Oops! Correct answer is: " + deck[self.cardnum].deflist[0])
    
        def nextword(self):
            self.cardnum += 1
            self.ui.lineEditAnswer.clear()
            self.ui.pushButtonEnter.setText("Enter")
            self.ui.labelVocab.setText(deck[self.cardnum].vocab)
            self.ui.pushButtonEnter.clicked.connect(self.checkanswer)
    
        if __name__ == "__main__":
            deck = parser.parser()
            app = QApplication(sys.argv)
            win = MainWindow()
            win.show()
            win.ui.labelVocab.setText(deck[MainWindow.cardnum].vocab)
        
            sys.exit(app.exec_())
    


  • In 'def checkanswer(self):' you are connecting to the clicked signal again and connecting to the slot nextword ( self.ui.pushButtonEnter.clicked.connect(self.nextword) ) and in 'def nextword(self):' you are connecting to the clicked signal and checkanswer slot ( self.ui.pushButtonEnter.clicked.connect(self.checkanswer) ). If you want to call 2 functions in the button click, connect the clicked signal to the two slots .

    def __init__(self):
            super().__init__()
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.pushButtonEnter.clicked.connect(self.checkanswer)
            self.ui.pushButtonEnter.clicked.connect(self.nextword)
            self.show()
    

    and remove self.ui.pushButtonEnter.clicked.connect(self.checkanswer) from 'def nextword(self)' and self.ui.pushButtonEnter.clicked.connect(self.nextword) from 'def checkanswer(self)'


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.