Search code examples
pythonpython-3.xtkinterpython-playsound

How to play a sound sample on every tkinter.text input?


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.


Solution

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