Getting a QPushButton to perform 2 actions?












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_())









share|improve this question























  • I was able to get it to work by checking if there was an input and only running the checkanswer() function if textvalue != "" but that doesn't change the fact that every push of this button will cause it to call the function. Is there any way to get around this?

    – M. Ruiz
    Nov 23 '18 at 12:18













  • Can each QPushButton do only one action? No. You can connect multiple signal handlers to every signal (including the clicked of push button). If you want to replace a signal handler, you have to disconnect the old before connecting the new.

    – Scheff
    Nov 23 '18 at 12:19











  • That makes a lot of sense and it seems to have worked. I wish you would have posted as an answer so I can flag it as the answer. I can't believe I haven't considered disconnecting.

    – M. Ruiz
    Nov 23 '18 at 12:23
















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_())









share|improve this question























  • I was able to get it to work by checking if there was an input and only running the checkanswer() function if textvalue != "" but that doesn't change the fact that every push of this button will cause it to call the function. Is there any way to get around this?

    – M. Ruiz
    Nov 23 '18 at 12:18













  • Can each QPushButton do only one action? No. You can connect multiple signal handlers to every signal (including the clicked of push button). If you want to replace a signal handler, you have to disconnect the old before connecting the new.

    – Scheff
    Nov 23 '18 at 12:19











  • That makes a lot of sense and it seems to have worked. I wish you would have posted as an answer so I can flag it as the answer. I can't believe I haven't considered disconnecting.

    – M. Ruiz
    Nov 23 '18 at 12:23














1












1








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_())









share|improve this question














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_())






python python-3.x qt user-interface pyqt5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 12:01









M. RuizM. Ruiz

306




306













  • I was able to get it to work by checking if there was an input and only running the checkanswer() function if textvalue != "" but that doesn't change the fact that every push of this button will cause it to call the function. Is there any way to get around this?

    – M. Ruiz
    Nov 23 '18 at 12:18













  • Can each QPushButton do only one action? No. You can connect multiple signal handlers to every signal (including the clicked of push button). If you want to replace a signal handler, you have to disconnect the old before connecting the new.

    – Scheff
    Nov 23 '18 at 12:19











  • That makes a lot of sense and it seems to have worked. I wish you would have posted as an answer so I can flag it as the answer. I can't believe I haven't considered disconnecting.

    – M. Ruiz
    Nov 23 '18 at 12:23



















  • I was able to get it to work by checking if there was an input and only running the checkanswer() function if textvalue != "" but that doesn't change the fact that every push of this button will cause it to call the function. Is there any way to get around this?

    – M. Ruiz
    Nov 23 '18 at 12:18













  • Can each QPushButton do only one action? No. You can connect multiple signal handlers to every signal (including the clicked of push button). If you want to replace a signal handler, you have to disconnect the old before connecting the new.

    – Scheff
    Nov 23 '18 at 12:19











  • That makes a lot of sense and it seems to have worked. I wish you would have posted as an answer so I can flag it as the answer. I can't believe I haven't considered disconnecting.

    – M. Ruiz
    Nov 23 '18 at 12:23

















I was able to get it to work by checking if there was an input and only running the checkanswer() function if textvalue != "" but that doesn't change the fact that every push of this button will cause it to call the function. Is there any way to get around this?

– M. Ruiz
Nov 23 '18 at 12:18







I was able to get it to work by checking if there was an input and only running the checkanswer() function if textvalue != "" but that doesn't change the fact that every push of this button will cause it to call the function. Is there any way to get around this?

– M. Ruiz
Nov 23 '18 at 12:18















Can each QPushButton do only one action? No. You can connect multiple signal handlers to every signal (including the clicked of push button). If you want to replace a signal handler, you have to disconnect the old before connecting the new.

– Scheff
Nov 23 '18 at 12:19





Can each QPushButton do only one action? No. You can connect multiple signal handlers to every signal (including the clicked of push button). If you want to replace a signal handler, you have to disconnect the old before connecting the new.

– Scheff
Nov 23 '18 at 12:19













That makes a lot of sense and it seems to have worked. I wish you would have posted as an answer so I can flag it as the answer. I can't believe I haven't considered disconnecting.

– M. Ruiz
Nov 23 '18 at 12:23





That makes a lot of sense and it seems to have worked. I wish you would have posted as an answer so I can flag it as the answer. I can't believe I haven't considered disconnecting.

– M. Ruiz
Nov 23 '18 at 12:23












2 Answers
2






active

oldest

votes


















3














First of all, there is a nice introduction in the Qt doc.:



Signals & Slots



which I warmly recommend to read.



Somewhere in the middle, you find the following paragraph:




By default, for every connection you make, a signal is emitted; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return false.




I must admit, I was not aware about the single disconnect(). Usually, I store the connection for a signal handler which is returned by connect() when I intend to disconnect it later. Furthermore, Qt allows also to disconnect a handler by the same arguments which were given in connect() but I never have used this.



Here my little Python sample for fiddling:



#!/usr/bin/python3

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton

app = QApplication(sys.argv)
qBtn1 = QPushButton("Click me")
qBtn1.show()

def onBtn1Clicked():
print("onBtn1Clicked")
qBtn1.clicked.disconnect()
qBtn1.setText("Click me again")
qBtn1.clicked.connect(onBtn1ClickedAgain)

def onBtn1ClickedAgain():
print("onBtn1ClickedAgain")
qBtn1.clicked.disconnect()
qBtn1.setText("Click me")
qBtn1.clicked.connect(onBtn1Clicked)

qBtn1.clicked.connect(onBtn1Clicked)
sys.exit(app.exec_())


The signal handlers onBtn1Clicked() and onBtn1ClickedAgain() connect each other mutual exclusive.



Tested in cygwin64 on Windows 10:



$ /usr/bin/python3 --version
Python 3.6.4

$ ./testQPushButton.py
onBtn1Clicked
onBtn1ClickedAgain
onBtn1Clicked
onBtn1ClickedAgain


Snapshot of testQPushButton.pySnapshot of testQPushButton.py after clicking button



Concerning the multiple calls of a signal handler, I recommend the following: comment the line qBtn1.clicked.disconnect() in both handlers and see what happens (on console).






share|improve this answer































    1














    As Scheff as pointed out in the comments, you can call the pushButton.disconnect() method and then connect clicked to another action.
    This is how I fixed my code:




    self.ui.pushButtonEnter.disconnect()
    self.ui.pushButtonEnter.clicked.connect(self.checkanswer)






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446352%2fgetting-a-qpushbutton-to-perform-2-actions%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      First of all, there is a nice introduction in the Qt doc.:



      Signals & Slots



      which I warmly recommend to read.



      Somewhere in the middle, you find the following paragraph:




      By default, for every connection you make, a signal is emitted; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return false.




      I must admit, I was not aware about the single disconnect(). Usually, I store the connection for a signal handler which is returned by connect() when I intend to disconnect it later. Furthermore, Qt allows also to disconnect a handler by the same arguments which were given in connect() but I never have used this.



      Here my little Python sample for fiddling:



      #!/usr/bin/python3

      import sys
      from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton

      app = QApplication(sys.argv)
      qBtn1 = QPushButton("Click me")
      qBtn1.show()

      def onBtn1Clicked():
      print("onBtn1Clicked")
      qBtn1.clicked.disconnect()
      qBtn1.setText("Click me again")
      qBtn1.clicked.connect(onBtn1ClickedAgain)

      def onBtn1ClickedAgain():
      print("onBtn1ClickedAgain")
      qBtn1.clicked.disconnect()
      qBtn1.setText("Click me")
      qBtn1.clicked.connect(onBtn1Clicked)

      qBtn1.clicked.connect(onBtn1Clicked)
      sys.exit(app.exec_())


      The signal handlers onBtn1Clicked() and onBtn1ClickedAgain() connect each other mutual exclusive.



      Tested in cygwin64 on Windows 10:



      $ /usr/bin/python3 --version
      Python 3.6.4

      $ ./testQPushButton.py
      onBtn1Clicked
      onBtn1ClickedAgain
      onBtn1Clicked
      onBtn1ClickedAgain


      Snapshot of testQPushButton.pySnapshot of testQPushButton.py after clicking button



      Concerning the multiple calls of a signal handler, I recommend the following: comment the line qBtn1.clicked.disconnect() in both handlers and see what happens (on console).






      share|improve this answer




























        3














        First of all, there is a nice introduction in the Qt doc.:



        Signals & Slots



        which I warmly recommend to read.



        Somewhere in the middle, you find the following paragraph:




        By default, for every connection you make, a signal is emitted; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return false.




        I must admit, I was not aware about the single disconnect(). Usually, I store the connection for a signal handler which is returned by connect() when I intend to disconnect it later. Furthermore, Qt allows also to disconnect a handler by the same arguments which were given in connect() but I never have used this.



        Here my little Python sample for fiddling:



        #!/usr/bin/python3

        import sys
        from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton

        app = QApplication(sys.argv)
        qBtn1 = QPushButton("Click me")
        qBtn1.show()

        def onBtn1Clicked():
        print("onBtn1Clicked")
        qBtn1.clicked.disconnect()
        qBtn1.setText("Click me again")
        qBtn1.clicked.connect(onBtn1ClickedAgain)

        def onBtn1ClickedAgain():
        print("onBtn1ClickedAgain")
        qBtn1.clicked.disconnect()
        qBtn1.setText("Click me")
        qBtn1.clicked.connect(onBtn1Clicked)

        qBtn1.clicked.connect(onBtn1Clicked)
        sys.exit(app.exec_())


        The signal handlers onBtn1Clicked() and onBtn1ClickedAgain() connect each other mutual exclusive.



        Tested in cygwin64 on Windows 10:



        $ /usr/bin/python3 --version
        Python 3.6.4

        $ ./testQPushButton.py
        onBtn1Clicked
        onBtn1ClickedAgain
        onBtn1Clicked
        onBtn1ClickedAgain


        Snapshot of testQPushButton.pySnapshot of testQPushButton.py after clicking button



        Concerning the multiple calls of a signal handler, I recommend the following: comment the line qBtn1.clicked.disconnect() in both handlers and see what happens (on console).






        share|improve this answer


























          3












          3








          3







          First of all, there is a nice introduction in the Qt doc.:



          Signals & Slots



          which I warmly recommend to read.



          Somewhere in the middle, you find the following paragraph:




          By default, for every connection you make, a signal is emitted; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return false.




          I must admit, I was not aware about the single disconnect(). Usually, I store the connection for a signal handler which is returned by connect() when I intend to disconnect it later. Furthermore, Qt allows also to disconnect a handler by the same arguments which were given in connect() but I never have used this.



          Here my little Python sample for fiddling:



          #!/usr/bin/python3

          import sys
          from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton

          app = QApplication(sys.argv)
          qBtn1 = QPushButton("Click me")
          qBtn1.show()

          def onBtn1Clicked():
          print("onBtn1Clicked")
          qBtn1.clicked.disconnect()
          qBtn1.setText("Click me again")
          qBtn1.clicked.connect(onBtn1ClickedAgain)

          def onBtn1ClickedAgain():
          print("onBtn1ClickedAgain")
          qBtn1.clicked.disconnect()
          qBtn1.setText("Click me")
          qBtn1.clicked.connect(onBtn1Clicked)

          qBtn1.clicked.connect(onBtn1Clicked)
          sys.exit(app.exec_())


          The signal handlers onBtn1Clicked() and onBtn1ClickedAgain() connect each other mutual exclusive.



          Tested in cygwin64 on Windows 10:



          $ /usr/bin/python3 --version
          Python 3.6.4

          $ ./testQPushButton.py
          onBtn1Clicked
          onBtn1ClickedAgain
          onBtn1Clicked
          onBtn1ClickedAgain


          Snapshot of testQPushButton.pySnapshot of testQPushButton.py after clicking button



          Concerning the multiple calls of a signal handler, I recommend the following: comment the line qBtn1.clicked.disconnect() in both handlers and see what happens (on console).






          share|improve this answer













          First of all, there is a nice introduction in the Qt doc.:



          Signals & Slots



          which I warmly recommend to read.



          Somewhere in the middle, you find the following paragraph:




          By default, for every connection you make, a signal is emitted; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return false.




          I must admit, I was not aware about the single disconnect(). Usually, I store the connection for a signal handler which is returned by connect() when I intend to disconnect it later. Furthermore, Qt allows also to disconnect a handler by the same arguments which were given in connect() but I never have used this.



          Here my little Python sample for fiddling:



          #!/usr/bin/python3

          import sys
          from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton

          app = QApplication(sys.argv)
          qBtn1 = QPushButton("Click me")
          qBtn1.show()

          def onBtn1Clicked():
          print("onBtn1Clicked")
          qBtn1.clicked.disconnect()
          qBtn1.setText("Click me again")
          qBtn1.clicked.connect(onBtn1ClickedAgain)

          def onBtn1ClickedAgain():
          print("onBtn1ClickedAgain")
          qBtn1.clicked.disconnect()
          qBtn1.setText("Click me")
          qBtn1.clicked.connect(onBtn1Clicked)

          qBtn1.clicked.connect(onBtn1Clicked)
          sys.exit(app.exec_())


          The signal handlers onBtn1Clicked() and onBtn1ClickedAgain() connect each other mutual exclusive.



          Tested in cygwin64 on Windows 10:



          $ /usr/bin/python3 --version
          Python 3.6.4

          $ ./testQPushButton.py
          onBtn1Clicked
          onBtn1ClickedAgain
          onBtn1Clicked
          onBtn1ClickedAgain


          Snapshot of testQPushButton.pySnapshot of testQPushButton.py after clicking button



          Concerning the multiple calls of a signal handler, I recommend the following: comment the line qBtn1.clicked.disconnect() in both handlers and see what happens (on console).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 12:58









          ScheffScheff

          7,85521325




          7,85521325

























              1














              As Scheff as pointed out in the comments, you can call the pushButton.disconnect() method and then connect clicked to another action.
              This is how I fixed my code:




              self.ui.pushButtonEnter.disconnect()
              self.ui.pushButtonEnter.clicked.connect(self.checkanswer)






              share|improve this answer




























                1














                As Scheff as pointed out in the comments, you can call the pushButton.disconnect() method and then connect clicked to another action.
                This is how I fixed my code:




                self.ui.pushButtonEnter.disconnect()
                self.ui.pushButtonEnter.clicked.connect(self.checkanswer)






                share|improve this answer


























                  1












                  1








                  1







                  As Scheff as pointed out in the comments, you can call the pushButton.disconnect() method and then connect clicked to another action.
                  This is how I fixed my code:




                  self.ui.pushButtonEnter.disconnect()
                  self.ui.pushButtonEnter.clicked.connect(self.checkanswer)






                  share|improve this answer













                  As Scheff as pointed out in the comments, you can call the pushButton.disconnect() method and then connect clicked to another action.
                  This is how I fixed my code:




                  self.ui.pushButtonEnter.disconnect()
                  self.ui.pushButtonEnter.clicked.connect(self.checkanswer)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 12:53









                  M. RuizM. Ruiz

                  306




                  306






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446352%2fgetting-a-qpushbutton-to-perform-2-actions%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno