How can I prevent the performance of the synthesizer from being interrupted?

0

I'm trying to make a program that, when I press a key, it reads which key was pressed.

For example:

Press the "a" key to return: "Normal Key A"

I press the "b" key to return: "Normal Key B" (in audio)

press key "7" returns: "Normal Key 7" (in audio)

press the "!" key. returns: "Ponctuation Key!" (in audio)

Press the "Shift" key to return: "Special Key Shift" (in audio)

For a reason that I do not know, it only works normally when the 1st key is pressed, from the second the audio is cut.

Please help me!

import pyttsx
engine = pyttsx.init()

from Tkinter import *

def key(event):
    if event.char == event.keysym:
        msg = 'Normal Key %r' % event.char
    elif len(event.char) == 1:
        msg = 'Punctuation Key %r (%r)' % (event.keysym, event.char)
    else:
        msg = 'Special Key %r' % event.keysym
    label1.config(text=msg)

    engine.say(msg)
    engine.runAndWait()

root = Tk()
prompt = '      Press any key      '
label1 = Label(root, text=prompt, width=len(prompt), bg='yellow')
label1.pack()

root.bind_all('<Key>', key)

root.mainloop()
    
asked by anonymous 02.12.2016 / 00:27

1 answer

0

To solve my problem, I just created the "engine" instance inside the "key" function and not the way I was doing it.

import pyttsx

from Tkinter import *

def key(event):
    engine = pyttsx.init()                  # A MUDANÇA ESTÁ AQUI
    if event.char == event.keysym:
        msg = 'Normal Key %r' % event.char
    elif len(event.char) == 1:
        msg = 'Punctuation Key %r (%r)' % (event.keysym, event.char)
    else:
        msg = 'Special Key %r' % event.keysym
    label1.config(text=msg)

    engine.say(msg)
    engine.runAndWait()

root = Tk()
prompt = '      Press any key      '
label1 = Label(root, text=prompt, width=len(prompt), bg='yellow')
label1.pack()

root.bind_all('<Key>', key)

root.mainloop()
    
02.12.2016 / 20:10