How to initiate an instance of pyttsx from within a class?

83 views Asked by At

I would like to make an instance from pyttsx inside a class and have a settings function to change the speech rate. But the init is not working properly as it gives the following error message:

AttributeError: TTSengine instance has no attribute 'say'

From my codes:

import pyttsx

class TTSengine():
    def __init__(self):
        self.engine = pyttsx.init()
    def settings(self):
        self.rate = self.engine.getProperty('rate')
        self.engine.setProperty('rate', self.rate-50)

y = pyttsx.init()
print y
y.say('I am ok')
y.runAndWait()

x = TTSengine()
print x
x.say('I am ok')
x.runAndWait()
1

There are 1 answers

0
Huzefa Mandviwala On

Your class itself doesn't have a say function. Your class has a pyttsx engine as a member variable, so the following should work:

x = TTSengine()
x.engine.say('I am ok')
x.engine.runAndWait()