### 16.1.6.7 Bindings and Events

The bind method from the widget command allows you to watch for certain events and to have a callback function trigger when that event type occurs. The form of the bind method is:

          def bind(self, sequence, func, add=''):

where:

sequence
  is a string that denotes the target kind of event. (See the bind man page and page 201 of John Ousterhout's book for details).

func
  is a Python function, taking one argument, to be invoked when the event occurs. An Event instance will be passed as the argument. (Functions deployed this way are commonly known as callbacks.)

add
  is optional, either "" or "`+`". Passing an empty string denotes that this binding is to replace any other bindings that this event is associated with. Preceeding with a "`+`" means that this function is to be added to the list of functions bound to this event type.

For example:

          def turnRed(self, event):
              event.widget["activeforeground"] = "red"

          self.button.bind("<Enter>", self.turnRed)

Notice how the widget field of the event is being accesed in the `turnRed()` callback. This field contains the widget that caught the X event. The following table lists the other event fields you can access, and how they are denoted in Tk, which can be useful when referring to the Tk man pages.

      Tk      Tkinter Event Field             Tk      Tkinter Event Field
      --      -------------------             --      -------------------
      %f      focus                           %A      char
      %h      height                          %E      send_event
      %k      keycode                         %K      keysym
      %s      state                           %N      keysym_num
      %t      time                            %T      type
      %w      width                           %W      widget
      %x      x                               %X      x_root
      %y      y                               %Y      y_root