Pyglet
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.
Warnings
- breaking Pyglet 2.0 (and later) requires modern OpenGL (3.3+) context, moving away from legacy OpenGL 2.0. Applications making direct OpenGL 1.x API calls or using older shaders will need significant updates.
- breaking Pyglet 2.1 introduced minor breaking changes to arguments for text, UI, and game controller handling. Locations/names for some features, data types, and math classes (e.g., `pyglet.math.Mat3` and `Mat4` now accept direct arguments instead of an iterable) also changed. The `pyglet.options` module is now accessed as a `pyglet.options` attribute.
- gotcha To play a wide range of compressed audio (e.g., MP3, OGG/Vorbis, WMA) and video formats (e.g., MPEG2, H.264), FFmpeg must be installed separately on your system. Without it, only basic formats (like WAV, PNG, BMP) are supported.
- gotcha When using `pyglet.clock.schedule_interval()` to schedule a function to run periodically, the scheduled function MUST accept a `dt` (delta time) parameter as its first argument, even if the value is not used within the function.
- gotcha Forgetting to call `window.clear()` at the beginning of your `on_draw` event handler will result in drawing artifacts from previous frames, as the buffer is not explicitly cleared.
Install
-
pip install pyglet
Imports
- pyglet
import pyglet
- Window
from pyglet.window import Window
- run
pyglet.app.run()
- schedule_interval
pyglet.clock.schedule_interval(func, interval)
Quickstart
import pyglet
window = pyglet.window.Window(800, 600, caption='Hello Pyglet')
label = pyglet.text.Label('Hello, world', font_name='Times New Roman', font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
@window.event
def on_draw():
window.clear()
label.draw()
pyglet.app.run()