Pyglet is a cross-platform windowing and multimedia library for Python, designed for developing games and other visually rich applications. It provides robust support for windowing, input event handling (keyboard, mouse, controllers), OpenGL graphics, loading various image and video formats, and playing sounds and music. Compatible with Windows, macOS, and Linux, pyglet is currently at version 2.1.14 and maintains an active development cycle with periodic updates and new releases.
pip install pygletVerified import paths — ran on the pinned version, not inferred.
This minimal example opens an 800x600 pixel window displaying 'Hello, world!' centered. It demonstrates window creation, text rendering, event handling via decorator, and starting the application loop.
Review and update OpenGL code to utilize modern OpenGL (3.3+) practices. Consult the 'Migrating from pyglet 1.5' guide in the official documentation.
Refer to the 'Migrating from pyglet 2.0' guide in the official documentation for specific changes and updated API usage.
Install FFmpeg on your operating system (e.g., via a package manager like `apt`, `brew`, or by downloading binaries) if you need to play advanced media formats.
Ensure your scheduled function signature includes `dt`, e.g., `def update(dt): ...`.
Always include `window.clear()` as the first line within your `on_draw` function.
Ensure pyglet is installed correctly for your Python environment: `pip install pyglet` (or `pip3 install pyglet` for Python 3 specific installations). If using a virtual environment or IDE, ensure it's activated and configured to use the correct Python interpreter where pyglet is installed.
Rename your Python script file to something other than `pyglet.py` (or any other pyglet module name like `window.py`, `gl.py`). For API changes, consult the pyglet documentation for the version you are using (e.g., `pyglet.window.Window` for creating a window, `pyglet.resource.image` for loading images, `pyglet.media.load` for loading audio/video).
Install the necessary OpenGL development libraries for your operating system. For Debian/Ubuntu-based systems, this often involves `sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev`. For other systems, refer to their package managers for OpenGL development packages.
Ensure `pyglet.app.run()` is called at the end of your main script after setting up your window and event handlers. This starts the main event loop. For example: `import pyglet; from pyglet.window import Window; window = Window(); @window.event def on_draw(): pass; pyglet.app.run()`.
If using a codebase designed for `pyglet 1.x`, downgrade your pyglet installation to a compatible version (e.g., `pip install pyglet==1.5.27`). Alternatively, update your code to use the `pyglet 2.x` API, which might involve importing constants from different submodules or using more modern OpenGL approaches (e.g., shaders instead of immediate mode functions like `GL_LIGHTING`).