python - 是带有打印功能的 pyttsx3 错误吗?
问题描述
这是一个简单的文本到语音程序。它所做的只是接收一个句子和一个说话者(不是来自用户),并在它应该说那个词的时候打印这个词。但是打印功能出现了问题(用# / 标记的那个)。当这个程序被执行时,我想在一行中打印这个句子。但是当 print function(# / mapped) 是参数 print(" ",end="") 它首先说出内容然后打印整行。
源代码:-
import pyttsx;
def onStar(name):
print(name+":-",end="")
def onWord(name, location, length):
for x in range(location,length+location+1) :
print(a[x],end="")
print() #*/ The function I am talking about.
#case1(works correctly) case2(does not work correctly[bug])
# print("") print("",end="")
# print()
# or just any print() without end as 2nd arg.
sentence=a='The quick brown fox jumped over the lazy dog.
speaker="narrator"
engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.say(a,speaker)
engine.runAndWait()
del engine
输出 :-
案例 1 单词与语音一起打印,但每个单词都在下一行
旁白:-
敏捷
的
棕色
狐狸
跳过
了
懒惰
的
狗。
案例2:- 文本正确打印,但在说出句子后才打印。
旁白:-敏捷的棕色狐狸跳过了懒惰的狗。
ps:-就像python不希望我打印行中的句子一样。
解决方案
设置flush=True
打印功能。
import pyttsx3
message = 'The quick brown fox jumped over the lazy dog.'
def onWord(name, location, length):
print(message[location:location + length], end=' ', flush=True)
engine = pyttsx3.init()
engine.connect('started-word', onWord)
engine.say(message)
engine.runAndWait()
推荐阅读
- html - 正确过渡共享边界
- c++ - 如何获取模板类型的内部类型?
- windows - VisualStudio C++ 创建的可执行文件不可部署在其他 Win10 计算机上(错误“0xc000007b”)
- angular - “AbstractControl”类型的参数不能分配给“FormControl”类型的参数
- ruby-on-rails - 修改现有 Rails 应用程序中的路线
- android - Fix warning NullPointerException in android
- c# - 强制执行 REAL 不可为空的字符串引用类型
- javascript - Integer comparison within score board app, score difference equal two
- javascript - Webassembly return c++ uint8_t array and pass that to javascript Uint8Array
- css - CSS 条件选择器 this 或 that