Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以同时使用print()函数和speak或say(如pyttsx3模块中的)吗?_Python_Pyttsx - Fatal编程技术网

Python 我可以同时使用print()函数和speak或say(如pyttsx3模块中的)吗?

Python 我可以同时使用print()函数和speak或say(如pyttsx3模块中的)吗?,python,pyttsx,Python,Pyttsx,我正试图找到一种方法,使通过print()函数在屏幕上打印的文本同时也能被说出。我目前正在使用pyttsx3模块,但我不知道如何才能做到这一点 我不知道在这种情况下我可以尝试什么。下面的代码只是一个示例代码 import pyttsx engine = pyttsx.init() print('Sally sells seashells by the seashore.') engine.say('Sally sells seashells by the seashore.') print(

我正试图找到一种方法,使通过
print()
函数在屏幕上打印的文本同时也能被说出。我目前正在使用
pyttsx3
模块,但我不知道如何才能做到这一点

我不知道在这种情况下我可以尝试什么。下面的代码只是一个示例代码

import pyttsx
engine = pyttsx.init()

print('Sally sells seashells by the seashore.')
engine.say('Sally sells seashells by the seashore.')

print('The quick brown fox jumped over the lazy dog.')
engine.say('The quick brown fox jumped over the lazy dog.')

engine.runAndWait()
我希望打印
引擎。说
命令一起工作。

在每个句子后使用
runAndWait()

如果您为此定义了一个函数,然后在要打印和说出的句子列表上迭代,那么代码可能会是这样的:

import pyttsx3

engine = pyttsx3.init() 

def print_and_speak(text):
    print(text)
    engine.say(text)
    engine.runAndWait()

text_list = ['Sally sells seashells by the seashore.',
             'The quick brown fox jumped over the lazy dog.']

for t in text_list:
    print_and_speak(t)