How to make the execution of the synthesizer not be interrupted?

Asked

Viewed 46 times

0

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

For example:

press the key "a" returns: "Normal Key A" (audio)

press "b" key returns: "Normal Key B" (audio)

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

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

press the "Shift" key returns: "Special Key Shift" (audio)

For a reason that I don’t know, it only works normally when the first keyboard is pressed,.

Please help me out!

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()
  • Isabella didn’t quite understand what you meant by: it only works normally when the 1st keypad is pressed could be a little clearer by kindness?

  • @cat when I press the A key, for example, I hear: "Normal Key A", when I press the A key again, I hear: "Nor" (audio does not run until the end.

1 answer

0


To solve my problem it was enough to create the "engine" instance inside the "key" function and not outside as I was doing.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.