我想写一个脚本,它会在将它们打印到屏幕时读取单词列表。
import pyttsx
engine = pyttsx.init()
words = ["here","are","some","test","words"]
for i in words:
engine.say(i)
print i
engine.runAndWait()
但是,在运行上述内容时,“here”以外的所有单词都会缩短。我听到“像这里[暂停]那样 - ” - “
如果我unindent engine.runAndWait()
,在循环完成后会说出这些词。当我这样做时,它们并没有被切断,但是,当然,在它们被打印的同时,它们并没有被说出来。
我正在运行Ubuntu 14.04.2
你想要的是打印单词,如何使用回调,使用pyttsx.Engine.connect
?
import pyttsx
def cb(name):
print(name)
engine = pyttsx.init()
engine.connect('started-utterance', cb)
for word in ["here", "are", "some", "test", "words"]:
engine.say(word, name=word)
engine.runAndWait()
这已经太晚了几年,但是在engine.startLoop(False)
的“外部事件循环”示例之后使用engine.iterate()
和docs为我做了这个工作。
import pyttsx
import time
engine = pyttsx.init()
words = ["here","are","some","test","words"]
engine.startLoop(False)
for i in words:
engine.say(i)
engine.iterate()
print i
while engine.isBusy(): # wait until finished talking
time.sleep(0.1)
engine.endLoop()