The application event loop

In order to let pyglet process operating system events such as mouse and keyboard events, applications need to enter an application event loop. The event loop continuously checks for new events, dispatches those events, and updates the contents of all open windows.

pyglet provides an application event loop that is tuned for performance and low power usage on Windows, Linux and Mac OS X. Most applications need only call:

pyglet.app.run()

to enter the event loop after creating their initial set of windows and attaching event handlers. The run function does not return until all open windows have been closed, or until pyglet.app.exit() is called.

The pyglet application event loop dispatches window events (such as for mouse and keyboard input) as they occur and dispatches the on_draw event to each window after every iteration through the loop.

To have additional code run periodically or every iteration through the loop, schedule functions on the clock (see Scheduling functions for future execution). pyglet ensures that the loop iterates only as often as necessary to fulfil all scheduled functions and user input.