What is PYGLET and why is it used?

What is PYGLET and why is it used?
What is PYGLET and why is it used?

Pyglet is a multimedia library for Python. It is basically used to build games and other visually rich applications. It works with window and Linux both.

It supports user interface, game controlling and joystick too. It allows downloading videos, images in almost any format which is quite a flexible feature of this. The library is created purely in python and is provided under the BSD open-source licence which helps to use commercial and open-source projects too.

Features provided by Pyglet:
There are several feature that this powerful library provides which includes:

  • Load images, videos, graphics in almost any format: We can load an image, video in any format that we want which is good flexibility provided by Pyglet. It uses FFmpeg to load the compressed audio, video.
  • No external Dependency or installation: There is no external dependency or installation needed for the development of most of the application which is a great feature that helps in simplifying installation.
  • Provided under BSD-open source license: It is provided under the BSD- open source licence which helps to use commercial and other open-source projects as witty some or null restrictions in that.
  • Supports Python2 & Python3 both: It supports Python2 and Python3 both which is quite good flexibility provided.
  • Advantage of multi-monitor desktop: For building games, sometimes we need a multi-monitor desktop and it is designed in such a way that it provides this feature in a flexible manner.

Installation: There can be many ways to install Pyglet. Since it is purely Python-based, it can be simply installed using a terminal.

Using: pip install pyglet

For Example:
import pyglet
window = pyglet.window.Window(width=640, height=480, caption=”Hello

Selecting a word in a caret: Let’s see how we can select a word in a caret using the module in Python. For games and multimedia, pyglet is a powerful library and is commonly used due to its flexibility.

What is a window?

  • A window is a “heighweight” object which occupies Os resources.
  • Windows normally fill the entire screen, that is it makes the screen full size.
  • Creating a window:

window = pyglet.window.Window(width, height, title)

Creating an empty window using Pyglet:

import pyglet

blog banner 1

# create a window

win = pyglet.window.Window();

run the pyglet app



What is a Caret?

  • Basically, it is a visible text insertion marker.
  • Drawn as a single vertical bar at the document position.
  • Creating a Caret: caret = pyglet.text.caret.Caret(layout, color=(255, 255, 255))

import pyglet
import pyglet.window.key

width of window

width = 500

height of window

height = 500

caption i.e title of the window

title = “Blog Writing”

creating a window

window = pyglet.window.Window(width, height, title)


text = “Welcome to GeeksforGeeks Have a nice day”

batch object

batch = pyglet.graphics.Batch()

creating document

document = pyglet.text.document.FormattedDocument(text)

setting style to the document

document.set_style(0, len(document.text), dict(
font_name =’Arial’, font_size = 16,
color =(255, 255, 255, 255)))

creating a incremental text layout

layout = pyglet.text.layout.IncrementalTextLayout(
document, 400, 350, batch = batch)

creating a caret

caret = pyglet.text.caret.Caret(layout, color =(255, 255, 255))

caret to window push handlers


moving caret to point

editable mark

value = caret.move_to_point(100, 30)

on draw event

def on_draw():

clear the window


draw the batch


caret to window push handlers


key press event

def on_key_press(symbol, modifier):

key “C” get press

if symbol == pyglet.window.key.C:

closing the window



image for icon

img = image = pyglet.resource.image(“logo.png”)

setting image as icon


selecting word which is nearest to 150, 100

caret.select_word(150, 100)

start running the application

Welcome to Blog Writing Have a nice day

Getting the latest development version of Pyglet:
To get the latest version of Pyglet, we can either choose HTTP or ssh to clone the latest directory structure:
In the case of Https: git clone https://github.com/pyglet/pyglet.git

In the case of ssh:
git clone git@github.com:pyglet/pyglet.git

Step By Step Writing an Pyglet Application:

Let’s begin with this by printing “Hello World”

  • Import its package
  • Create a window, by calling a default constructor, window = pyglet.window.Window()
  • Now to display the text with a particular font, size and colour, just create a label

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’

  • Attaching event handler

def on_draw();

  • Now lastly calling: pyglet.app.run()

Advantages of Pyglet:

As discussed,pyglet is completely based on python and supports gaming and multimedia. We can highlight the main advantage would be:

  • Completely built on Python: As this is completely built on python, thus it is quite easy and flexible to use this.
  • Easy Installation and Support to different OS: Well the installation process is quite easy which makes it the best choice for gaming and multimedia. It supports Linux, windows too which is good flexibility to be used.
  • No External Dependency: There is no external dependency on the installation requirement of pyglet.

To read more about Python, click here.

By Deepak Jain