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:

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

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

@win.event
def on_draw():
    # ... drawing code ...

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

from pyglet import app
app.run()

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)
win.set_exclusive_mouse()

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
else:
    win = window.Window(config=configs[0])

Modules

event Events for pyglet.window.
key Key constants and utilities for pyglet.window.
mouse Mouse constants and utilities for pyglet.window.

Classes

DefaultMouseCursor The default mouse cursor used by the operating system.
Display A display device supporting one or more screens.
FPSDisplay Display of a window’s framerate.
ImageMouseCursor A user-defined mouse cursor created from an image.
MouseCursor An abstract mouse cursor.
Platform Operating-system-level functionality.
Window Platform-independent application window.

Exceptions

MouseCursorException The root exception for all mouse cursor-related errors.
NoSuchConfigException An exception indicating the requested configuration is not
NoSuchDisplayException An exception indicating the requested display is not available.
NoSuchScreenModeException An exception indicating the requested screen resolution could not be
WindowException The root exception for all window-related errors.

Functions

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

Notes

Defined

  • gl
  • pprint
  • pyglet
  • sys