You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行pyttsx3代码报TypeError: 'Engine' object is not callable如何解决

问题原因定位
  • 对象调用逻辑错误:pyt.init()返回的是pyttsx3的Engine类实例,你将该实例赋值给了speak变量后直接以speak("xxx")的形式调用,但Engine实例本身不是可调用对象,才会触发'Engine' object is not callable的类型报错。正确的调用方式是使用Engine实例自带的say()方法传入待播报文本,调用后还需执行runAndWait()方法,等待语音队列全部处理完成再执行后续逻辑。
  • 时间判断逻辑冗余错误:你编写的elif hour >=0 and hour<12 and hour<18条件永远无法命中,该范围已经被前面的if hour>= 0 and hour<12完全覆盖,需要调整判断条件的区间。
修复后可运行代码
import pyttsx3 as pyt
import datetime as DT
hour = int(DT.datetime.now().hour)
print(hour)

# 初始化语音引擎
engine = pyt.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

# 封装播报方法适配原有调用习惯
def speak(text):
    engine.say(text)
    engine.runAndWait()

if hour >= 0 and hour < 12:
    speak("Good Morning Sir !")
elif hour >= 12 and hour < 18:
    speak("Good Afternoon Sir !")
# 可自行补充晚间问候逻辑
else:
    speak("Good Evening Sir !")

内容的提问来源于stack exchange,提问作者Mahmoud Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 14:45:03