Module pyglet.event

Event dispatch framework.

All objects that produce events in pyglet implement EventDispatcher, providing a consistent interface for registering and manipulating event handlers. A commonly used event dispatcher is pyglet.window.Window.

Event types

For each event dispatcher there is a set of events that it dispatches; these correspond with the type of event handlers you can attach. Event types are identified by their name, for example, ''on_resize''. If you are creating a new class which implements EventDispatcher, you must call EventDispatcher.register_event_type for each event type.

Attaching event handlers

An event handler is simply a function or method. You can attach an event handler by setting the appropriate function on the instance:

def on_resize(width, height):
    # ...
dispatcher.on_resize = on_resize

There is also a convenience decorator that reduces typing:

def on_resize(width, height):
    # ...

You may prefer to subclass and override the event handlers instead:

class MyDispatcher(DispatcherClass):
    def on_resize(self, width, height):
        # ...

Event handler stack

When attaching an event handler to a dispatcher using the above methods, it replaces any existing handler (causing the original handler to no longer be called). Each dispatcher maintains a stack of event handlers, allowing you to insert an event handler "above" the existing one rather than replacing it.

There are two main use cases for "pushing" event handlers:

Use EventDispatcher.push_handlers to create a new level in the stack and attach handlers to it. You can push several handlers at once:

dispatcher.push_handlers(on_resize, on_key_press)

If your function handlers have different names to the events they handle, use keyword arguments:


After an event handler has processed an event, it is passed on to the next-lowest event handler, unless the handler returns EVENT_HANDLED, which prevents further propogation.

To remove all handlers on the top stack level, use EventDispatcher.pop_handlers.

Note that any handlers pushed onto the stack have precedence over the handlers set directly on the instance (for example, using the methods described in the previous section), regardless of when they were set. For example, handler foo is called before handler bar in the following example:

dispatcher.on_resize = bar

Dispatching events

pyglet uses a single-threaded model for all application code. Event handlers are only ever invoked as a result of calling EventDispatcher.dispatch_events`.

It is up to the specific event dispatcher to queue relevant events until they can be dispatched, at which point the handlers are called in the order the events were originally generated.

This implies that your application runs with a main loop that continously updates the application state and checks for new events:

while True:
    # ... additional per-frame processing

Not all event dispatchers require the call to dispatch_events; check with the particular class documentation.


An exception raised when an event handler could not be attached.
Generic event dispatcher interface.