python tkinter how to bind key to a button

You’ll need to make two changes:

  1. Add

    master.bind('s', self.sharpen)
    

    to __init__. (Binding to the Frame, self, does not seem to work.)

  2. When s is pressed, self.sharpen(event) will be called. Since
    Tkinter will be sending a Tkinter.Event object, we must also change the call
    signature to

    def sharpen(self, event=None):
    

    Thus, when the button is pressed, event will be set to the default
    value, None, but when the s key is pressed, event
    will be assigned to the Tkinter.Event object.

Leave a Comment