pyglet.window

Submodules

Details

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=960, height=540)

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 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 = pyglet.canvas.get_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])

Classes

class Window

Bases: EventDispatcher

Platform-independent application window.

A window is a “heavyweight” object occupying operating system resources. The “client” or “content” area of a window is filled entirely with an OpenGL viewport. Applications have no access to operating system widgets or controls; all rendering must be done via OpenGL.

Windows may appear as floating regions or can be set to fill an entire screen (fullscreen). When floating, windows may appear borderless or decorated with a platform-specific frame (including, for example, the title bar, minimize and close buttons, resize handles, and so on).

While it is possible to set the location of a window, it is recommended that applications allow the platform to place it according to local conventions. This will ensure it is not obscured by other windows, and appears on an appropriate screen for the user.

To render into a window, you must first call its switch_to() method to make it the active OpenGL context. If you use only one window in your application, you can skip this step as it will always be the active context.

Methods

activate()

Attempt to restore keyboard focus to the window.

Depending on the window manager or operating system, this may not be successful. For example, on Windows XP an application is not allowed to “steal” focus from another application. Instead, the window’s taskbar icon will flash, indicating it requires attention.

static clear()

Clear the window.

This is a convenience method for clearing the color and depth buffer. The window must be the active context (see switch_to()).

close()

Close the window.

After closing the window, the GL context will be invalid. The window instance cannot be reused once closed. To re-use windows, see set_visible() instead.

The pyglet.app.EventLoop.on_window_close() event is dispatched by the pyglet.app.event_loop when this method is called.

dispatch_event(*args)

Dispatch an event to the attached event handlers.

The event is propagated to all registered event handlers in the stack, starting and the top and going down. If any registered event handler returns EVENT_HANDLED, no further handlers down the stack will receive this event.

This method has several possible return values. If any event hander has returned EVENT_HANDLED, then this method will also return EVENT_HANDLED. If not, this method will return EVENT_UNHANDLED. If there were no events registered to receive this event, False is returned.

Parameters:
event_typestr

The name of the event to dispatch.

argssequence

Optional arguments to pass to the event handlers.

Return type:

bool or None

Returns:

EVENT_HANDLED if any event handler returned EVENT_HANDLED; EVENT_UNHANDLED if one or more event handlers were invoked without any of them returning EVENT_HANDLED; False if no event handlers were registered.

dispatch_events()

Poll the operating system event queue for new events and call attached event handlers.

This method is provided for legacy applications targeting pyglet 1.0, and advanced applications that must integrate their event loop into another framework.

Typical applications should use pyglet.app.run().

draw_mouse_cursor()

Draw the custom mouse cursor.

If the current mouse cursor has drawable set, this method is called before the buffers are flipped to render it.

There is little need to override this method; instead, subclass MouseCursor and provide your own draw() method.

flip()

Swap the OpenGL front and back buffers.

Call this method on a double-buffered window to update the visible display with the back buffer. Windows are double-buffered by default unless you turn this feature off.

The contents of the back buffer are undefined after this operation.

The default event_loop automatically calls this method after the window’s on_draw() event.

get_framebuffer_size()

Return the size in actual pixels of the Window framebuffer.

When using HiDPI screens, the size of the Window’s framebuffer can be higher than that of the Window size requested. If you are performing operations that require knowing the actual number of pixels in the window, this method should be used instead of Window.get_size(). For example, setting the Window projection or setting the glViewport size.

Return type:

(int, int)

Returns:

The width and height of the Window’s framebuffer, in pixels.

get_location()

Return the current position of the window.

Return type:

(int, int)

Returns:

The distances of the left and top edges from their respective edges on the virtual desktop, in pixels.

get_pixel_ratio()

Return the framebuffer/window size ratio.

Some platforms and/or window systems support subpixel scaling, making the framebuffer size larger than the window size. Retina screens on OS X and Gnome on Linux are some examples.

On a Retina systems the returned ratio would usually be 2.0 as a window of size 500 x 500 would have a framebuffer of 1000 x 1000. Fractional values between 1.0 and 2.0, as well as values above 2.0 may also be encountered.

Return type:

float

Returns:

The framebuffer/window size ratio

get_size() Tuple[int, int]

Return the current size of the window.

This does not include the windows’ border or title bar.

Return type:

(int, int)

Returns:

The width and height of the window, in pixels.

get_system_mouse_cursor(name)

Obtain a system mouse cursor.

Use set_mouse_cursor to make the cursor returned by this method active. The names accepted by this method are the CURSOR_* constants defined on this class.

Parameters:
namestr

Name describing the mouse cursor to return. For example, CURSOR_WAIT, CURSOR_HELP, etc.

Return type:

MouseCursor

Returns:

A mouse cursor which can be used with set_mouse_cursor.

maximize()

Maximize the window.

The behaviour of this method is somewhat dependent on the user’s display setup. On a multi-monitor system, the window may maximize to either a single screen or the entire virtual desktop.

minimize()

Minimize the window.

set_caption(caption)

Set the window’s caption.

The caption appears in the titlebar of the window, if it has one, and in the taskbar on Windows and many X11 window managers.

Parameters:
captionstr or unicode

The caption to set.

set_exclusive_keyboard(exclusive=True)

Prevent the user from switching away from this window using keyboard accelerators.

When enabled, this feature disables certain operating-system specific key combinations such as Alt+Tab (Command+Tab on OS X). This can be useful in certain kiosk applications, it should be avoided in general applications or games.

Parameters:
exclusivebool

If True, exclusive keyboard is enabled, otherwise it is disabled.

set_exclusive_mouse(exclusive=True)

Hide the mouse cursor and direct all mouse events to this window.

When enabled, this feature prevents the mouse leaving the window. It is useful for certain styles of games that require complete control of the mouse. The position of the mouse as reported in subsequent events is meaningless when exclusive mouse is enabled; you should only use the relative motion parameters dx and dy.

Parameters:
exclusivebool

If True, exclusive mouse is enabled, otherwise it is disabled.

set_fullscreen(
fullscreen=True,
screen=None,
mode=None,
width=None,
height=None,
)

Toggle to or from fullscreen.

After toggling fullscreen, the GL context should have retained its state and objects, however the buffers will need to be cleared and redrawn.

If width and height are specified and fullscreen is True, the screen may be switched to a different resolution that most closely matches the given size. If the resolution doesn’t match exactly, a higher resolution is selected and the window will be centered within a black border covering the rest of the screen.

Parameters:
fullscreenbool

True if the window should be made fullscreen, False if it should be windowed.

screenScreen

If not None and fullscreen is True, the window is moved to the given screen. The screen must belong to the same display as the window.

modeScreenMode

The screen will be switched to the given mode. The mode must have been obtained by enumerating Screen.get_modes. If None, an appropriate mode will be selected from the given width and height.

widthint

Optional width of the window. If unspecified, defaults to the previous window size when windowed, or the screen size if fullscreen.

New in version 1.2.

heightint

Optional height of the window. If unspecified, defaults to the previous window size when windowed, or the screen size if fullscreen.

New in version 1.2.

set_icon(*images)

Set the window icon.

If multiple images are provided, one with an appropriate size will be selected (if the correct size is not provided, the image will be scaled).

Useful sizes to provide are 16x16, 32x32, 64x64 (Mac only) and 128x128 (Mac only).

Parameters:
imagessequence of pyglet.image.AbstractImage

List of images to use for the window icon.

set_location(x, y)

Set the position of the window.

Parameters:
xint

Distance of the left edge of the window from the left edge of the virtual desktop, in pixels.

yint

Distance of the top edge of the window from the top edge of the virtual desktop, in pixels.

set_maximum_size(width: int, height: int) None

Set the maximum size of the window.

Once set, the user will not be able to resize the window larger than the given dimensions. There is no way to remove the maximum size constraint on a window (but you could set it to a large value).

The behaviour is undefined if the maximum size is set smaller than the current size of the window.

The window size does not include the border or title bar.

Parameters:
widthint

Maximum width of the window, in pixels.

heightint

Maximum height of the window, in pixels.

Return type:

None

set_minimum_size(width: int, height: int) None

Set the minimum size of the window.

Once set, the user will not be able to resize the window smaller than the given dimensions. There is no way to remove the minimum size constraint on a window (but you could set it to 0,0).

The behaviour is undefined if the minimum size is set larger than the current size of the window.

The window size does not include the border or title bar.

Parameters:
widthint

Minimum width of the window, in pixels.

heightint

Minimum height of the window, in pixels.

Return type:

None

set_mouse_cursor(cursor=None)

Change the appearance of the mouse cursor.

The appearance of the mouse cursor is only changed while it is within this window.

Parameters:
cursorMouseCursor

The cursor to set, or None to restore the default cursor.

set_mouse_platform_visible(platform_visible=None)

Set the platform-drawn mouse cursor visibility. This is called automatically after changing the mouse cursor or exclusive mode.

Applications should not normally need to call this method, see set_mouse_visible instead.

Parameters:
platform_visiblebool or None

If None, sets platform visibility to the required visibility for the current exclusive mode and cursor type. Otherwise, a bool value will override and force a visibility.

set_mouse_visible(visible=True)

Show or hide the mouse cursor.

The mouse cursor will only be hidden while it is positioned within this window. Mouse events will still be processed as usual.

Parameters:
visiblebool

If True, the mouse cursor will be visible, otherwise it will be hidden.

set_size(width: int, height: int) None

Resize the window.

The behaviour is undefined if the window is not resizable, or if it is currently fullscreen.

The window size does not include the border or title bar.

Parameters:
widthint

New width of the window, in pixels.

heightint

New height of the window, in pixels.

Return type:

None

set_visible(visible: bool = True) None

Show or hide the window.

Parameters:
visiblebool

If True, the window will be shown; otherwise it will be hidden.

Return type:

None

switch_to()

Make this window the current OpenGL rendering context.

Only one OpenGL context can be active at a time. This method sets the current window context as the active one.

In most cases, you should use this method instead of directly calling pyglet.gl.Context.set_current(). The latter will not perform platform-specific state management tasks for you.

Events

on_activate()

The window was activated.

This event can be triggered by clicking on the title bar, bringing it to the foreground; or by some platform-specific method.

When a window is “active” it has the keyboard focus.

Event:

on_close()

The user attempted to close the window.

This event can be triggered by clicking on the “X” control box in the window title bar, or by some other platform-dependent manner.

The default handler sets has_exit to True. In pyglet 1.1, if pyglet.app.event_loop is being used, close is also called, closing the window immediately.

Event:

on_context_lost()

The window’s GL context was lost.

When the context is lost no more GL methods can be called until it is recreated. This is a rare event, triggered perhaps by the user switching to an incompatible video mode. When it occurs, an application will need to reload all objects (display lists, texture objects, shaders) as well as restore the GL state.

Event:

on_context_state_lost()

The state of the window’s GL context was lost.

pyglet may sometimes need to recreate the window’s GL context if the window is moved to another video device, or between fullscreen or windowed mode. In this case it will try to share the objects (display lists, texture objects, shaders) between the old and new contexts. If this is possible, only the current state of the GL context is lost, and the application should simply restore state.

Event:

on_deactivate()

The window was deactivated.

This event can be triggered by clicking on another application window. When a window is deactivated it no longer has the keyboard focus.

Event:

on_draw()

The window contents should be redrawn.

The EventLoop will dispatch this event when the draw method has been called. The window will already have the GL context, so there is no need to call switch_to. The window’s flip method will be called immediately after this event, so your event handler should not.

You should make no assumptions about the window contents when this event is triggered; a resize or expose event may have invalidated the framebuffer since the last time it was drawn.

New in version 1.1.

Event:

on_expose()

A portion of the window needs to be redrawn.

This event is triggered when the window first appears, and any time the contents of the window is invalidated due to another window obscuring it.

There is no way to determine which portion of the window needs redrawing. Note that the use of this method is becoming increasingly uncommon, as newer window managers composite windows automatically and keep a backing store of the window contents.

Event:

on_file_drop(x, y, paths)

File(s) were dropped into the window, will return the position of the cursor and a list of paths to the files that were dropped.

New in version 1.5.1.

Event:

on_hide()

The window was hidden.

This event is triggered when a window is minimised or hidden by the user.

Event:

on_key_press(symbol, modifiers)

A key on the keyboard was pressed (and held down).

Since pyglet 1.1 the default handler dispatches the on_close() event if the ESC key is pressed.

Parameters:
symbolint

The key symbol pressed.

modifiersint

Bitwise combination of the key modifiers active.

Event:

on_key_release(symbol, modifiers)

A key on the keyboard was released.

Parameters:
symbolint

The key symbol pressed.

modifiersint

Bitwise combination of the key modifiers active.

Event:

on_mouse_drag(x, y, dx, dy, buttons, modifiers)

The mouse was moved with one or more mouse buttons pressed.

This event will continue to be fired even if the mouse leaves the window, so long as the drag buttons are continuously held down.

Parameters:
xint

Distance in pixels from the left edge of the window.

yint

Distance in pixels from the bottom edge of the window.

dxint

Relative X position from the previous mouse position.

dyint

Relative Y position from the previous mouse position.

buttonsint

Bitwise combination of the mouse buttons currently pressed.

modifiersint

Bitwise combination of any keyboard modifiers currently active.

Event:

on_mouse_enter(x, y)

The mouse was moved into the window.

This event will not be triggered if the mouse is currently being dragged.

Parameters:
xint

Distance in pixels from the left edge of the window.

yint

Distance in pixels from the bottom edge of the window.

Event:

on_mouse_leave(x, y)

The mouse was moved outside the window.

This event will not be triggered if the mouse is currently being dragged. Note that the coordinates of the mouse pointer will be outside the window rectangle.

Parameters:
xint

Distance in pixels from the left edge of the window.

yint

Distance in pixels from the bottom edge of the window.

Event:

on_mouse_motion(x, y, dx, dy)

The mouse was moved with no buttons held down.

Parameters:
xint

Distance in pixels from the left edge of the window.

yint

Distance in pixels from the bottom edge of the window.

dxint

Relative X position from the previous mouse position.

dyint

Relative Y position from the previous mouse position.

Event:

on_mouse_press(x, y, button, modifiers)

A mouse button was pressed (and held down).

Parameters:
xint

Distance in pixels from the left edge of the window.

yint

Distance in pixels from the bottom edge of the window.

buttonint

The mouse button that was pressed.

modifiersint

Bitwise combination of any keyboard modifiers currently active.

Event:

on_mouse_release(x, y, button, modifiers)

A mouse button was released.

Parameters:
xint

Distance in pixels from the left edge of the window.

yint

Distance in pixels from the bottom edge of the window.

buttonint

The mouse button that was released.

modifiersint

Bitwise combination of any keyboard modifiers currently active.

Event:

on_mouse_scroll(x, y, scroll_x, scroll_y)

The mouse wheel was scrolled.

Note that most mice have only a vertical scroll wheel, so scroll_x is usually 0. An exception to this is the Apple Mighty Mouse, which has a mouse ball in place of the wheel which allows both scroll_x and scroll_y movement.

Parameters:
xint

Distance in pixels from the left edge of the window.

yint

Distance in pixels from the bottom edge of the window.

scroll_xfloat

Amount of movement on the horizontal axis.

scroll_yfloat

Amount of movement on the vertical axis.

Event:

on_move(x, y)

The window was moved.

Parameters:
xint

Distance from the left edge of the screen to the left edge of the window.

yint

Distance from the top edge of the screen to the top edge of the window. Note that this is one of few methods in pyglet which use a Y-down coordinate system.

Event:

on_refresh(dt)

The window contents should be redrawn.

The EventLoop will dispatch this event when the draw method has been called. The window will already have the GL context, so there is no need to call switch_to. The window’s flip method will be called immediately after this event, so your event handler should not.

You should make no assumptions about the window contents when this event is triggered; a resize or expose event may have invalidated the framebuffer since the last time it was drawn.

New in version 2.0.

Event:

on_resize(width, height)

The window was resized.

The window will have the GL context when this event is dispatched; there is no need to call switch_to in this handler.

Parameters:
widthint

The new width of the window, in pixels.

heightint

The new height of the window, in pixels.

Event:

on_show()

The window was shown.

This event is triggered when a window is restored after being minimised, hidden, or after being displayed for the first time.

Event:

on_text(text)

The user input some text.

Typically this is called after on_key_press() and before on_key_release(), but may also be called multiple times if the key is held down (key repeating); or called without key presses if another input method was used (e.g., a pen input).

You should always use this method for interpreting text, as the key symbols often have complex mappings to their unicode representation which this event takes care of.

Parameters:
textunicode

The text entered by the user.

Event:

on_text_motion(motion)

The user moved the text input cursor.

Typically this is called after on_key_press() and before on_key_release(), but may also be called multiple times if the key is help down (key repeating).

You should always use this method for moving the text input cursor (caret), as different platforms have different default keyboard mappings, and key repeats are handled correctly.

The values that motion can take are defined in pyglet.window.key:

  • MOTION_UP

  • MOTION_RIGHT

  • MOTION_DOWN

  • MOTION_LEFT

  • MOTION_NEXT_WORD

  • MOTION_PREVIOUS_WORD

  • MOTION_BEGINNING_OF_LINE

  • MOTION_END_OF_LINE

  • MOTION_NEXT_PAGE

  • MOTION_PREVIOUS_PAGE

  • MOTION_BEGINNING_OF_FILE

  • MOTION_END_OF_FILE

  • MOTION_BACKSPACE

  • MOTION_DELETE

Parameters:
motionint

The direction of motion; see remarks.

Event:

on_text_motion_select(motion)

The user moved the text input cursor while extending the selection.

Typically this is called after on_key_press() and before on_key_release(), but may also be called multiple times if the key is help down (key repeating).

You should always use this method for responding to text selection events rather than the raw on_key_press(), as different platforms have different default keyboard mappings, and key repeats are handled correctly.

The values that motion can take are defined in pyglet.window.key:

  • MOTION_UP

  • MOTION_RIGHT

  • MOTION_DOWN

  • MOTION_LEFT

  • MOTION_NEXT_WORD

  • MOTION_PREVIOUS_WORD

  • MOTION_BEGINNING_OF_LINE

  • MOTION_END_OF_LINE

  • MOTION_NEXT_PAGE

  • MOTION_PREVIOUS_PAGE

  • MOTION_BEGINNING_OF_FILE

  • MOTION_END_OF_FILE

Parameters:
motionint

The direction of selection motion; see remarks.

Event:

Attributes

aspect_ratio

The aspect ratio of the window. Read-Only.

Type:

float

caption

The window caption (title). Read-only.

Type:

str

config

A GL config describing the context of this window. Read-only.

Type:

pyglet.gl.Config

context

The OpenGL context attached to this window. Read-only.

Type:

pyglet.gl.Context

display

The display this window belongs to. Read-only.

Type:

Display

fullscreen

True if the window is currently fullscreen. Read-only.

Type:

bool

has_exit = False
height

The height of the window, in pixels. Read-write.

Type:

int

invalid = True
projection

The OpenGL window projection matrix. Read-write.

This matrix is used to transform vertices when using any of the built-in drawable classes. view is done first, then projection.

The default projection matrix is orthographic (2D), but a custom pyglet.math.Mat4 instance can be set. Alternatively, you can supply a flat tuple of 16 values.

(2D), but can be changed to any 4x4 matrix desired. See pyglet.math.Mat4.

Type:

pyglet.math.Mat4

resizeable

True if the window is resizable. Read-only.

Type:

bool

screen

The screen this window is fullscreen in. Read-only.

Type:

Screen

size

The size of the window. Read-Write.

Type:

tuple

style

The window style; one of the WINDOW_STYLE_* constants. Read-only.

Type:

int

view

The OpenGL window view matrix. Read-write.

This matrix is used to transform vertices when using any of the built-in drawable classes. view is done first, then projection.

The default view is an identity matrix, but a custom pyglet.math.Mat4 instance can be set. Alternatively, you can supply a flat tuple of 16 values.

Type:

pyglet.math.Mat4

viewport

The Window viewport

The Window viewport, expressed as (x, y, width, height).

Return type:

(int, int, int, int)

Returns:

The viewport size as a tuple of four ints.

visible

True if the window is currently visible. Read-only.

Type:

bool

vsync

True if buffer flips are synchronised to the screen’s vertical retrace. Read-only.

Type:

bool

width

The width of the window, in pixels. Read-write.

Type:

int

Class attributes: cursor names

CURSOR_CROSSHAIR = 'crosshair'
CURSOR_DEFAULT = None
CURSOR_HAND = 'hand'
CURSOR_HELP = 'help'
CURSOR_NO = 'no'
CURSOR_SIZE = 'size'
CURSOR_SIZE_DOWN = 'size_down'
CURSOR_SIZE_DOWN_LEFT = 'size_down_left'
CURSOR_SIZE_DOWN_RIGHT = 'size_down_right'
CURSOR_SIZE_LEFT = 'size_left'
CURSOR_SIZE_LEFT_RIGHT = 'size_left_right'
CURSOR_SIZE_RIGHT = 'size_right'
CURSOR_SIZE_UP = 'size_up'
CURSOR_SIZE_UP_DOWN = 'size_up_down'
CURSOR_SIZE_UP_LEFT = 'size_up_left'
CURSOR_SIZE_UP_RIGHT = 'size_up_right'
CURSOR_TEXT = 'text'
CURSOR_WAIT = 'wait'
CURSOR_WAIT_ARROW = 'wait_arrow'

Class attributes: window styles

WINDOW_STYLE_BORDERLESS = 'borderless'
WINDOW_STYLE_DEFAULT = None
WINDOW_STYLE_DIALOG = 'dialog'
WINDOW_STYLE_OVERLAY = 'overlay'
WINDOW_STYLE_TOOL = 'tool'
WINDOW_STYLE_TRANSPARENT = 'transparent'
__init__(
width=None,
height=None,
caption=None,
resizable=False,
style=None,
fullscreen=False,
visible=True,
vsync=True,
file_drops=False,
display=None,
screen=None,
config=None,
context=None,
mode=None,
)

Create a window.

All parameters are optional, and reasonable defaults are assumed where they are not specified.

The display, screen, config and context parameters form a hierarchy of control: there is no need to specify more than one of these. For example, if you specify screen the display will be inferred, and a default config and context will be created.

config is a special case; it can be a template created by the user specifying the attributes desired, or it can be a complete config as returned from Screen.get_matching_configs or similar.

The context will be active as soon as the window is created, as if switch_to was just called.

Parameters:
widthint

Width of the window, in pixels. Defaults to 960, or the screen width if fullscreen is True.

heightint

Height of the window, in pixels. Defaults to 540, or the screen height if fullscreen is True.

captionstr or unicode

Initial caption (title) of the window. Defaults to sys.argv[0].

resizablebool

If True, the window will be resizable. Defaults to False.

styleint

One of the WINDOW_STYLE_* constants specifying the border style of the window.

fullscreenbool

If True, the window will cover the entire screen rather than floating. Defaults to False.

visiblebool

Determines if the window is visible immediately after creation. Defaults to True. Set this to False if you would like to change attributes of the window before having it appear to the user.

vsyncbool

If True, buffer flips are synchronised to the primary screen’s vertical retrace, eliminating flicker.

displayDisplay

The display device to use. Useful only under X11.

screenScreen

The screen to use, if in fullscreen.

configpyglet.gl.Config

Either a template from which to create a complete config, or a complete config.

contextpyglet.gl.Context

The context to attach to this window. The context must not already be attached to another window.

modeScreenMode

The screen will be switched to this mode if fullscreen is True. If None, an appropriate mode is selected to accomodate width and height.

__new__(**kwargs)
class FPSDisplay

Bases: object

Display of a window’s framerate.

This is a convenience class to aid in profiling and debugging. Typical usage is to create an FPSDisplay for each window, and draw the display at the end of the windows’ on_draw() event handler:

from pyglet.window import Window, FPSDisplay

window = Window()
fps_display = FPSDisplay(window)

@window.event
def on_draw():
    # ... perform ordinary window drawing operations ...

    fps_display.draw()

The style and position of the display can be modified via the Label() attribute. Different text can be substituted by overriding the set_fps method. The display can be set to update more or less often by setting the update_period attribute. Note: setting the update_period to a value smaller than your Window refresh rate will cause inaccurate readings.

Ivariables:
labelLabel

The text label displaying the framerate.

__init__(window, color=(127, 127, 127, 127), samples=240)
draw()

Draw the label.

update()

Records a new data point at the current time. This method is called automatically when the window buffer is flipped.

update_period = 0.25

Time in seconds between updates.

Type:

float

class MouseCursor

An abstract mouse cursor.

draw(x, y)

Abstract render method.

The cursor should be drawn with the “hot” spot at the given coordinates. The projection is set to the pyglet default (i.e., orthographic in window-space), however no other aspects of the state can be assumed.

Parameters:
xint

X coordinate of the mouse pointer’s hot spot.

yint

Y coordinate of the mouse pointer’s hot spot.

gl_drawable = True

Indicates if the cursor is drawn using OpenGL, or natively.

hw_drawable = False
class DefaultMouseCursor

Bases: MouseCursor

The default mouse cursor set by the operating system.

__init__()
__new__(**kwargs)
class ImageMouseCursor

Bases: MouseCursor

A user-defined mouse cursor created from an image.

Use this class to create your own mouse cursors and assign them to windows. Cursors can be drawn by OpenGL, or optionally passed to the OS to render natively. There are no restrictions on cursors drawn by OpenGL, but natively rendered cursors may have some platform limitations (such as color depth, or size). In general, reasonably sized cursors will render correctly

__init__(image, hot_x=0, hot_y=0, acceleration=False)

Create a mouse cursor from an image.

Parameters:
imagepyglet.image.AbstractImage

Image to use for the mouse cursor. It must have a valid texture attribute.

hot_xint

X coordinate of the “hot” spot in the image relative to the image’s anchor. May be clamped to the maximum image width if acceleration=True.

hot_yint

Y coordinate of the “hot” spot in the image, relative to the image’s anchor. May be clamped to the maximum image height if acceleration=True.

accelerationint

If True, draw the cursor natively instead of usign OpenGL. The image may be downsampled or color reduced to fit the platform limitations.

draw(x, y)

Abstract render method.

The cursor should be drawn with the “hot” spot at the given coordinates. The projection is set to the pyglet default (i.e., orthographic in window-space), however no other aspects of the state can be assumed.

Parameters:
xint

X coordinate of the mouse pointer’s hot spot.

yint

Y coordinate of the mouse pointer’s hot spot.

Exceptions

class MouseCursorException

The root exception for all mouse cursor-related errors.

__init__(*args, **kwargs)
__new__(**kwargs)
class NoSuchConfigException

An exception indicating the requested configuration is not available.

__init__(*args, **kwargs)
__new__(**kwargs)
class NoSuchDisplayException

An exception indicating the requested display is not available.

__init__(*args, **kwargs)
__new__(**kwargs)
class WindowException

The root exception for all window-related errors.

__init__(*args, **kwargs)
__new__(**kwargs)