When running my python app, pressing the keys does not produce a sound however once you exit the tkinter gui, it works and plays the sound. I tried to re position the code but then the GUI doesn't appear at all.
from Tkinter import *
import winsound
import pythoncom
import pyHook
root = Tk()
root.geometry("500x500")
root.title("Piano Keys")
photo = PhotoImage(file="food.gif")
picture = Label(root, image=photo)
picture.pack()
start = Button(root, text='Start Piano Keys')
close = Button(root, text='Exit Piano Keys', command=lambda:root.destroy())
close.pack()
root.mainloop()
def OnKeyboardEvent(event):
key = event.Key
if key == 'A':
winsound.Beep(261, 200)
if key == 'S':
winsound.Beep(277, 200)
if key == 'D':
winsound.Beep(293, 200)
if key == 'F':
winsound.Beep(311, 200)
if key == 'G':
winsound.Beep(329, 200)
if key == 'H':
winsound.Beep(349, 200)
if key == 'J':
winsound.Beep(370, 200)
if key == 'K':
winsound.Beep(392, 200)
if key == 'L':
winsound.Beep(415, 200)
return True
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()