Package pyglet.window

Windowing and user-interface events.

This module allows applications to create and display windows with an OpenGL context. Windows can be created with a variety of border styles or set fullscreen.

You can register event handlers for keyboard, mouse and window events. For games and kiosks you can also restrict the input to your windows, for example disabling users from switching away from the application with certain key combinations or capturing and hiding the mouse.

Getting started

Call the Window constructor to create a new window:

from pyglet.window import Window
win = Window(width=640, height=480)

Attach your own event handlers:

def on_key_press(symbol, modifiers):
    # ... handle this event ...

Place drawing code for the window within the Window.on_draw event handler:

def on_draw():
    # ... drawing code ...

Call to enter the main event loop (by default, this returns when all open windows are closed):

from pyglet import app

Creating a game window

Use Window.set_exclusive_mouse to hide the mouse cursor and receive relative mouse movement events. Specify fullscreen=True as a keyword argument to the Window constructor to render to the entire screen rather than opening a window:

win = Window(fullscreen=True)

Working with multiple screens

By default, fullscreen windows are opened on the primary display (typically set by the user in their operating system settings). You can retrieve a list of attached screens and select one manually if you prefer. This is useful for opening a fullscreen window on each screen:

display = window.get_platform().get_default_display()
screens = display.get_screens()
windows = []
for screen in screens:
    windows.append(window.Window(fullscreen=True, screen=screen))

Specifying a screen has no effect if the window is not fullscreen.

Specifying the OpenGL context properties

Each window has its own context which is created when the window is created. You can specify the properties of the context before it is created by creating a "template" configuration:

from pyglet import gl
# Create template config
config = gl.Config()
config.stencil_size = 8
config.aux_buffers = 4
# Create a window using this config
win = window.Window(config=config)

To determine if a given configuration is supported, query the screen (see above, "Working with multiple screens"):

configs = screen.get_matching_configs(config)
if not configs:
    # ... config is not supported
    win = window.Window(config=configs[0])


Events for pyglet.window.
Key constants and utilities for pyglet.window.
Mouse constants and utilities for pyglet.window.


The root exception for all window-related errors.
An exception indicating the requested display is not available.
An exception indicating the requested configuration is not available.
The root exception for all mouse cursor-related errors.
Operating-system-level functionality.
A display device supporting one or more screens.
A virtual monitor that supports fullscreen windows.
An abstract mouse cursor.
The default mouse cursor used by the operating system.
A user-defined mouse cursor created from an image.
Platform-independent application window.


Platform get_platform()
Get an instance of the Platform most appropriate for this system.


  __package__ = 'pyglet.window'

Function Details


Get an instance of the Platform most appropriate for this system.
Platform: The platform instance.