Skip to main content
1 of 8
user3.1415927
  • 171
  • 1
  • 1
  • 6

Is there a better (more pythonic) way to bind a keyboard key to a GUI button?

I have bound some keyboard keys to functions:

    # Bind a key to each button from above.
    master.bind('q', quit)
    master.bind('f', self.f)

I have some buttons in the GUI:

    # Create and load two buttons in to the above-created frame.
    self.button = Button(frame, text="QUIT", fg="red", command=quit)
    self.button.pack(side=LEFT)
    self.slogan = Button(frame, text="Hello", command = self.f)
    self.slogan.pack(side=RIGHT)

I have defined the f function, and quit is a built-in:

def f(self, event=None):
    print 'in f()'
    return 'hello world'

Is this the proper(, pythonic) way to bind a keyboard key and GUI button? (In my previous pseudocode I would have bound the keyboard button to the actual GUI button, but that didn't seem possible.)

The bindings are defined in the Class init . Also, (as may be evident from indentation,) the function is defined outside the init, but still inside of the Class. [Should I just post the entire file here? It's only 84 lines and 18 of that is header comments..]

user3.1415927
  • 171
  • 1
  • 1
  • 6