How to bind a keypress to a button in Tkinter

后端 未结 1 468
深忆病人
深忆病人 2020-12-18 15:46

My Senior Project involves a robot I can control over wifi. I am using a Raspberry Pi and a Tkinter window to send commands to the robot. I have the rough draft of my Tkinte

相关标签:
1条回答
  • 2020-12-18 16:23

    I believe what you want is to bind the key press to the frame/function. Tkinter has its own event and binding handling built in which you can read up on here.

    Here is quick example which you should be able to adapt your program.

    from tkinter import *
    
    root = Tk()
    
    def yourFunction(event):
        print('left')
    
    frame = Frame(root, width=100, height=100)
    
    frame.bind("<Left>",yourFunction)   #Binds the "left" key to the frame and exexutes yourFunction if "left" key was pressed
    frame.pack()
    
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题