Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Getting 1 QPushButton to perform 2 different function calls
Forum Updated to NodeBB v4.3 + New Features

Getting 1 QPushButton to perform 2 different function calls

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 620 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kyouma
    wrote on last edited by
    #1

    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_())
    
    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      Yaswanth
      wrote on last edited by Yaswanth
      #2

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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved