TypeError: 'Engine' object is not callable

TypeError: 'Engine' object is not callable

import pyttsx3 as pyt
import datetime as DT
hour = int(DT.datetime.now().hour)
print(hour)

# enter code here

speak = pyt.init()
voices = speak.getProperty('voices')
speak.setProperty('voice', voices[1].id)

if hour>= 0 and hour<12:
    speak("Good Morning Sir !")
elif hour >=0 and hour<12 and hour<18:
    speak("Good Afternoon Sir !") 

您需要调用 speak.say 而不是像这样 speak

if hour>= 0 and hour<12:
    speak.say("Good Morning Sir !")
elif hour >=0 and hour<12 and hour<18:
    speak.say("Good Afternoon Sir !") 

此外,您应该将 speak 重命名为 engine 以更具描述性。这是来自 the docs 的示例:

import pyttsx3
engine = pyttsx3.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()