Search code examples
pythontkinterkeyboard

keyboard binding in piano python


how can I bind keyboard in piano using python GUI?

I tried using bind but I did not know how to use that.

btnCs = Button(ABC2,  height = 6, width = 6, bd = 4, text = "C#", font =('arial', 18 , 'bold'), bg = "black",  fg = "white", command = value_Cs)

I expect an example using the code given help me pls


Solution

  • Watch the console output running your function when you press the a key in your keyboard. You need to focus the tkinter window with the mouse. event=None is needed because the binded callback function gets passed an event object by the tkinter event loop (mainloop):

    from tkinter import Tk
    
    def play_a(event=None):
        print('Play the A key sound')
    
    root = Tk()
    root.bind('a', play_a)
    
    root.mainloop()