问题描述
在我的 python 3.8.5 虚拟环境中使用 pyttsx3 模块时,此代码如下:
source_0
import pyttsx3
def speak(speak):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
engine.getProperty('rate')
engine.setProperty('rate',155)
engine.say(speak)
engine.runAndWait()
speak("Hello")
每次运行给定的python代码时,都会发生上述错误,并且我在Windows 10中使用VS代码作为我的编码IDE。
解决方法
将pyttsx3.init()
从函数中取出。它不叫重复。它只能被调用一次。
import pyttsx3
engine = pyttsx3.init()
def speak(word):
#set engine properties
engine.say(word)
engine.runAndWait()
speak('Speak')
,
这应该有效:
import pyttsx3
engine = pyttsx3.init()
def speak(word):
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate',155)
engine.say(word)
engine.runAndWait()
speak("Hello")