3

I'm developing a chat program using Python-3.5 and Qt Creator for the GUI.

At first when Enter is pressed it calls the first function called run_chat. So far so good. But when the if statement is true, I want to get the next value entered by the user and use it in another function.

The problem is, when the statement is true and I press Enter, the first function is called again!

The code:

B = cursor.execute('SELECT Respond FROM Welcoming_Engine ORDER BY  RANDOM()').fetchone()

class Ui(QtWidgets.QDialog):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('chatbotgui.ui', self)
        self.show() 
         #--------------------------------------------#
        self.textBrowser.append('المساعد:  ' + B[0])
        #self.pushButton.clicked.connect(self.run_chat)
        self.lineEdit.returnPressed.connect(self.run_chat)
         #--------------------------------------------#

    def exit(self):
        H= self.lineEdit.text()
        self.textBrowser.append('انت:  ' + H)
        classifier1 = nltk.NaiveBayesClassifier.train(t1)
        test_sent_features = {word.lower(): (word in word_tokenize(H)) for word in all_words1}
        sent_result= classifier1.classify(test_sent_features)
        if sent_result == 'Positive':
            B= cursor.execute('SELECT Respond FROM Goodbye_Engine ORDER BY RANDOM()').fetchone()
            cursor.execute('DELETE FROM chatting_log')
            connection.commit()
            self.textBrowser.append('المساعد:  ' + B[0])
        else:
            B= "اسف!! سوف اعمل على تطوير نفسي"
            self.textBrowser.append('المساعد:  ' + B[0])   

    def run_chat(self):
        H= self.lineEdit.text()
        self.textBrowser.append('انت:  ' + H)
        self.lineEdit.clear()
        if re.search(r'باي|الى اللقاء',H):
            B ='تقييمك لعملي؟'
            self.textBrowser.append('المساعد:  ' + B)
            self.lineEdit.returnPressed.connect(self.exit)
        else:
            New_H= PreProcess_text(H)
            self.textBrowser.append('المساعد:  ' + New_H)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Ui()
    sys.exit(app.exec_())  
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Eman S.
  • 145
  • 1
  • 8

2 Answers2

0

You have to disconnect the signal first before connecting it to another slot:

 if re.search(r'باي|الى اللقاء',H):
     ...
     self.lineEdit.returnPressed.disconnect(self.run_chat)
     self.lineEdit.returnPressed.connect(self.exit)
Leistungsabfall
  • 6,368
  • 7
  • 33
  • 41
0

A signal can be connected multiple times, so when you re-connect returnPressed in run_chat, it will not remove the previous connections. You must therefore explicitly remove the existing connections first:

    try:
        self.lineEdit.returnPressed.disconnect()
    except TypeError:
        pass
    self.lineEdit.returnPressed.connect(self.exit)

NB: disconnect will raise an error if there are no current connections.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336