Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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 是pyttsx3带有打印功能的bug吗?_Python_Debugging_Printing_Pyttsx - Fatal编程技术网

Python 是pyttsx3带有打印功能的bug吗?

Python 是pyttsx3带有打印功能的bug吗?,python,debugging,printing,pyttsx,Python,Debugging,Printing,Pyttsx,这里是一个简单的文本到语音的程序。它所做的只是接收一个句子和一个说话人(不是来自用户)并在它应该说这个词的时候打印这个词。但是打印函数(用#/)出现了问题。当这个程序被执行时,我想在一行中打印这个句子。但是当print函数(#/marked)是参数print(“,end=”“)时,它首先说出内容,然后打印整行内容 源代码:- import pyttsx; def onStar(name): print(name+":-",end="") def onWord(name,

这里是一个简单的文本到语音的程序。它所做的只是接收一个句子和一个说话人(不是来自用户)并在它应该说这个词的时候打印这个词。但是打印函数(用#/)出现了问题。当这个程序被执行时,我想在一行中打印这个句子。但是当print函数(#/marked)是参数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()

无法运行此代码。也许你想发布一个复制你的问题的帖子。它工作得很好。但有趣的是,从engine.connect运行代码…。到engine.runAndWait()n次将在句子中打印单词n次。例如,如果我在这个注释中两次运行上面突出显示的代码。我的输出将是“敏捷的棕色狐狸跳过了懒惰的狗”