Next / Previous / Contents

54.2. Event sequences

Tkinter has a powerful and general method for allowing you to define exactly which events, both specific and general, you want to bind to handlers.

In general, an event sequence is a string containing one or more event patterns. Each event pattern describes one thing that can happen. If there is more than one event pattern in a sequence, the handler will be called only when all the patterns happen in that same sequence.

The general form of an event pattern is:

<[modifier-]...type[-detail]>
  • The entire pattern is enclosed inside <…>.

  • The event type describes the general kind of event, such as a key press or mouse click. See Section 54.3, “Event types”.

  • You can add optional modifier items before the type to specify combinations such as the shift or control keys being depressed during other key presses or mouse clicks. Section 54.4, “Event modifiers”

  • You can add optional detail items to describe what key or mouse button you're looking for. For mouse buttons, this is 1 for button 1, 2 for button 2, or 3 for button 3.

    • The usual setup has button 1 on the left and button 3 on the right, but left-handers can swap these positions.

    • For keys on the keyboard, this is either the key's character (for single-character keys like the A or * key) or the key's name; see Section 54.5, “Key names” for a list of all key names.

Here are some examples to give you the flavor of event patterns:

<Button-1> The user pressed the first mouse button.
<KeyPress-H> The user pressed the H key.
<Control-Shift-KeyPress-H> The user pressed control-shift-H.