I need to play a typewriter key sample on every tkinter.text input. I came across the Playsound module but I don't know how to listen to the inputs.
Asked
Active
Viewed 52 times
2 Answers
1
You would use a bind
and setup a function to play the sound when the bind is triggered.
import tkinter as tk
def key(event):
print("pressed", repr(event.char))
# Play sound here
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.bind('<Key>', key)
root.mainloop()

tgikal
- 1,667
- 1
- 13
- 27
0
Thanks, I've come up with a very similar solution tho its really really laggy. Im basically writing a simple typewriter emulator so the key sound is reproduced for each typed letter.
import tkinter as tk
from PIL import Image, ImageTk
from playsound import playsound
def key(event):
key = event.char
playsound("C:/Users/Isma/key1.mp3")
win = tk.Tk()
frame = tk.Frame(win, width=300, height=400)
frame.grid(row=1, column=0)
text = tk.Text(frame)
text.grid(row=0,column=0)
text.bind('<Key>',lambda a : key(a))
image = Image.open("C:/Users/Isma/swintec1.jpg")
photo = ImageTk.PhotoImage(image)
label = tk.Label(frame,image=photo)
label.image = photo
label.grid(row=3,column=0)
win.mainloop()
-
1Use `playsound("C:/Users/Isma/key1.mp3", block = False)` – tgikal Mar 21 '20 at 00:02
-
@tgikal Thanks a lot, solved the issue which i was trying to tackle by reducing the sound sample quality and lenght. – Esmail Alberto Ghassemi Mar 21 '20 at 00:11