1

我正在用 python 做一个虚拟助手。我用 pyttsx3 跟我说话。我以 oops 的方式编写了 pyttsx3 初始化。但是当我运行这段代码时,它什么也没说。这是我的代码。

import pyttsx3

class Speaker:
    
    def __init__(self):
        self.engine = pyttsx3.init() # Initializing pyttsx3
        self.voices = self.engine.getProperty('voices')  # Getting voices property
    
    def speaker_config(self):
        ''' Configurations for speaker '''   
        self.engine.getProperty('voices')  
        self.engine.setProperty('voice', self.voices[1].id)  # Changing voice male to female
        self.engine.setProperty('rate', 140)  # Changing the speed
    
    def say(self):
        self.engine.say('hello')
    
s1 = Speaker()
s1.say()```

4

1 回答 1

2

在执行以下操作后,您还需要添加engine.runAndWait()以处理所有排队的命令engine.say

def say(self):
    self.engine.say('hello')
    self.engine.runAndWait()

请参阅文档

于 2021-01-10T06:55:15.543 回答