首页 > 解决方案 > 是带有打印功能的 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不希望我打印行中的句子一样。

标签: pythondebuggingprintingpyttsx

解决方案


设置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()

推荐阅读