如何修复python 3.8中的pyttsx3模块错误

问题描述

在我的 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")

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...