Python Pyglet Module
Pyglet is a cross-platform windowing and multimedia library for Python. It is intended for developing games and other visually rich applications. It has no external dependencies or installation requirements.
Installation
pip install pyglet
Basic Window
import pyglet
window = pyglet.window.Window(width=800, height=600, caption="Hello Pyglet")
@window.event
def on_draw():
window.clear()
pyglet.app.run()
Displaying Text
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()
Handling Input
from pyglet.window import key
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.A:
print('The "A" key was pressed.')
elif symbol == key.LEFT:
print('The left arrow key was pressed.')
Working with Images
image = pyglet.image.load('kitten.png')
@window.event
def on_draw():
window.clear()
image.blit(0, 0)
Playing Sound
Pyglet can play WAV and other audio formats.
music = pyglet.resource.media('music.mp3')
music.play()