0

I am making Gui for my chatbot in pyqt but i have a bit problem in this area of code.

def __init__(self):
    super(Window, self).__init__()
    self.setGeometry(50, 50, 500, 300)
    self.setWindowTitle("Chatbot 0.3")


def offline_speak(chat_speech):
    engine = pyttsx.init()
    engine.say(chat_speech)
    engine.runAndWait()

few things will change like def offline_speak(self) then mention it in above init like self.offline_speak() but i have no idea about engine code.

Can anyone suggest me anything?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

It is not necessary to make offline_speak() a method of the class, but this task can be very time consuming so it can block the mainloop generated by Qt, so it is advisable to execute it in a second thread as I show with the help of QRunnable and QThreadPool

import pyttsx

from PyQt4.QtGui import *
from PyQt4.QtCore import *


class SpeechRunnable(QRunnable):
    def __init__(self):
        QRunnable.__init__(self)
    def run(self):
        self.engine = pyttsx.init()
        self.engine.say(self.chat_speech)
        self.engine.runAndWait()

    def say(self, text):
        self.chat_speech = text
        QThreadPool.globalInstance().start(self)

    def stop(self):
        self.engine.stop()


class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.runnable = None
        self.setWindowTitle("Chatbot 0.3")
        lay = QVBoxLayout(self)
        self.le = QLineEdit(text, self)
        self.btnStart = QPushButton("start", self)
        self.btnStop = QPushButton("stop", self)
        self.btnStart.clicked.connect(self.onClickedStart)
        lay.addWidget(self.le)
        lay.addWidget(self.btnStart)
        lay.addWidget(self.btnStop)


    def onClickedStart(self):
        self.runnable = SpeechRunnable()
        self.runnable.say(self.le.text())
        self.btnStop.clicked.connect(self.runnable.stop)

    def closeEvent(self, event):
        if self.runnable is not None:
            self.runnable.stop()
            QThread.msleep(100) #delay
        super(Window, self).closeEvent(event)
text = """

What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
It has survived not only five centuries, but also the leap into electronic typesetting, 
remaining essentially unchanged. 
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, 
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
"""


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you for the reply got it but the problem is the speech doesn't stops even the program is closed. And can i save and run it through mp3 files? Thanks again :) – Luffy Games Nov 21 '17 at 16:44
  • I have already corrected the error, check it and if it worked, do not forget to mark it as correct. – eyllanesc Nov 22 '17 at 18:11
  • Thank you but i am using PyQt4 and i changed the PyQt5 to PyQt4 but don't have the QtWidget. So should i go with PyQt.Gui or something? – Luffy Games Nov 22 '17 at 18:41
  • Also in `class Window(QWidget):` isn't it QtWidget ? – Luffy Games Nov 22 '17 at 18:43
  • You must place the necessary tags, in your case pyqt4, if you do not do it many of us will assume that you are using pyqt5. and update my answer. – eyllanesc Nov 22 '17 at 18:44
  • QtWidgets is a sub-package within PyQt5, QWidget is the basic widget of all pyqt. – eyllanesc Nov 22 '17 at 18:45
  • @eyllansec Thank you for the answers. And sorry for the trouble. :) – Luffy Games Nov 22 '17 at 18:49
  • I know this is an old question, but replacing these with PyQt5 doesn't work for me, and I keep having the same problem when using pyttsx3 with PyQt5: The program immediately skips to the `sys.exit(app.exec())` line, and my program exits without any traceback or errors. Do you know why this may be so? – Dugbug May 29 '21 at 23:57
  • @Dugbug Recommendation: Don't use any IDE and run your script from the console or CMD to get the error message. – eyllanesc May 29 '21 at 23:58
  • I tried that too, and I still don't get any error messages. I tried making a simple program where a PyQt5 pushbutton is connected to a function that uses `pyttsx3` to speak "Hello World", and the result is it speaking the text and immediately skipping to the `sys.exit(app.exec())` line at the end of my program. If this function were to have just a `print()` function, it would work without exiting. Do you know why this may be so? – Dugbug Jun 01 '21 at 16:21