Dispatching events manually

Earlier versions of pyglet and certain other windowing toolkits such as PyGame and SDL require the application developer to write their own event loop. This "manual" event loop is usually just an inconvenience compared to pyglet.app.run, but can be necessary in some situations when combining pyglet with other toolkits.

A simple event loop usually has the following form:

while True:
    pyglet.clock.tick()

    for window in pyglet.app.windows:
        window.switch_to()
        window.dispatch_events()
        window.dispatch_event('on_draw')
        window.flip()

The dispatch_events method checks the window's operating system event queue for user input and dispatches any events found. The method does not wait for input -- if ther are no events pending, control is returned to the program immediately.

The call to pyglet.clock.tick() is required for ensuring scheduled functions are called, including the internal data pump functions for playing sounds and video.

Developers are strongly discouraged from writing pyglet applications with event loops like this:

The capability for writing manual event loops remains for legacy support and extreme circumstances.