AttributeError: 'Engine' object has no attribute 'runandwait'

AttributeError: 'Engine' object has no attribute 'runandwait'

下面的程序出错。这是我的程序:

import pyttsx3 as tts

def speak(text):
    engine = tts.init("sapi5")
    engine.say(text)
    engine.runandwait()

speak('Hello user this is a test message.')

这是我收到的错误消息:

Traceback (most recent call last):
  File "c:\Users\SIDDHESH\Projects\test.py", line 6, in <module>
    speak('Hello user this is a test message.')
  File "c:\Users\SIDDHESH\Projects\test.py", line 5, in speak
    engine.runandwait()
AttributeError: 'Engine' object has no attribute 'runandwait'

我想指出的是,我的变量名为 engine,但错误消息说的是变量 'Engine'。我还想指出,我正在使用 Visual Studio 代码 到 运行 我的代码。

看来您需要编写 engine.runAndWait(),每个新单词都以大写字母开头。请记住 python 区分大小写,这意味着 runandwaitrunAndWait 是不同的属性。