pyglet is a library for the Python programming language that provides an object-oriented application programming interface for the creation of games and other multimedia applications.
pyglet runs on Microsoft Windows, macOS, and Linux; it is released under the BSD Licence.
pyglet was first created by Alex Holkner.
It supports windowed and full-screen operations as well as multiple monitors. Images, video, and sound files in a range of formats can be done natively, with more additional capabilities supplied by the optional
AVbin plugin, which uses the
Libav package to provide support for audio formats including
Ogg/Vorbis, and Windows Media Audio, and video formats such as
Comparison to other libraries
pygame is another library used for making games and is much more widely known than
pyglet. This is what it would take to display the window shown in the example. It takes more lines of code and also is harder to understand.
pygame is not object-oriented and has less functionality. For example, there is no rich and formatted text, fast drawing commands, etc. The graphics and images have to be drawn again and again and can not be cached. This is one of the performance features of object-oriented programming, where most of the steps do not have to be repeated
import pygame screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Hello world!") running = True while running: for event in pygame.event.get(): if event.type in pygame.QUIT: running = False pygame.quit()
As a multimedia library,
pyglet comes with batteries included. Using OpenGL gives it speed benefits, and is also written entirely in Python, meaning no external dependencies that have to be installed. The
ffmpeg library can be optionally installed to support more audio formats.
Text display and formatting
- Rich text formatting (bold, italic, underline, color change, background color, indent, lists) (
- Built-in layouts to support editable text
- Carets (
- HTML support (
Image and sprite work
- Fast image processing and rendering
- Built-in sprites (
- Animated images (
- OpenGL shaders supported
- Simple built-in shapes (rectangles, circles, triangles) (
- Batched rendering (
- 3D model rendering
Events and file system
- Resource management (
- Clock for processing events and time (
- Window events (
- Event dispatchers for your own event dispatching (
- Context management
These features make
pyglet much faster than many other multimedia libraries. Batched rendering is a technique to draw multiple objects with the same amount of time. Batches support
Sprites, images, and
TextLayouts, which are the basis for all labels and text. Text functions are implemented as well. Multi-level lists are supported, and can be made using HTML. Different parts of the displayed document can have different styles. A caret is built-in for support for editing text. It resembles many of the features of a UI text input caret.
from pyglet.window import Window from pyglet.app import run window = Window(caption="Hello world!", width=640, height=480) run()
In this example, lines 1-2 import the
pyglet module's necessary components. Line 4 creates a window, and line 6 calls
pyglet to run its event loop. Optionally an update rate (in frames per second) can be specified in a
- Pygame, another Python game API, a layer over Simple DirectMedia Layer
- Kivy (framework), a Python OpenGL-based UI for multitouch interactions