Skip to main content
 首页 » 编程设计

python之Pyttsx "run"但没有 "wait"

2025年05月04日9birdshome

我正在使用 pyttsx在我的游戏中,但我遇到了一个问题 - 方法 runAndWait() 导致它停止一小段时间来说出排队的文本。这是个问题,因为它打乱了我的时间计算。是否可以说一段文字而不停止所有其他事件?或者在 python/pygame 中是否有任何其他文本到语音转换器?

def say(text): 
   voices = engine.getProperty('voices') 
   engine.setProperty('voice', voices[1].id) 
   engine.setProperty('rate', 250) 
   engine.say(text) 
   engine.runAndWait() 

请您参考如下方法:

Pyttsx 没有像 runAndNoWait() 这样的方法,但您可以将 say(text) 函数作为单独的线程运行以停止阻塞主游戏线程。

import threading 
text = 'Hello world' 
threading.Thread(target=say, args=(text,)).start()