pyglet.Window

Methods

Window.__init__(**kwargs)[source]

Initialize a window instance.

Parameters:
  • title (str) – The window title

  • gl_version (tuple) – Major and minor version of the opengl context to create

  • size (tuple) – Window size x, y

  • resizable (bool) – Should the window be resizable?

  • fullscreen (bool) – Open window in fullscreen mode

  • vsync (bool) – Enable/disable vsync

  • aspect_ratio (float) – The desired fixed aspect ratio. Can be set to None to make aspect ratio be based on the actual window size.

  • samples (int) – Number of MSAA samples for the default framebuffer

  • cursor (bool) – Enable/disable displaying the cursor inside the window

Window.init_mgl_context() None

Create or assign a ModernGL context. If no context is supplied a context will be created using the window’s gl_version.

Keyword Arguments:

ctx – An optional custom ModernGL context

Window.is_key_pressed(key) bool

Returns: The press state of a key

Window.set_icon(icon_path: str) None

Sets the window icon to the given path

Parameters:

icon_path (str) – path to the icon

Window.close() None[source]

Close the pyglet window directly

Window.use()

Bind the window’s framebuffer

Window.clear(red=0.0, green=0.0, blue=0.0, alpha=0.0, depth=1.0, viewport=None)

Binds and clears the default framebuffer

Parameters:
  • red (float) – color component

  • green (float) – color component

  • blue (float) – color component

  • alpha (float) – alpha component

  • depth (float) – depth value

  • viewport (tuple) – The viewport

Window.render(time=0.0, frame_time=0.0) None

Renders a frame by calling the configured render callback

Keyword Arguments:
  • time (float) – Current time in seconds

  • frame_time (float) – Delta time from last frame in seconds

Window.swap_buffers() None[source]

Swap buffers, increment frame counter and pull events

Window.resize(width, height) None

Should be called every time window is resized so the example can adapt to the new size if needed

Window.destroy()[source]

Destroy the pyglet window

Window.set_default_viewport() None

Calculates the and sets the viewport based on window configuration.

The viewport will based on the configured fixed aspect ratio if set. If no fixed aspect ratio is set the viewport will be scaled to the entire window size regardless of size.

Will add black borders and center the viewport if the window do not match the configured viewport (fixed only)

Window.convert_window_coordinates(x, y, x_flipped=False, y_flipped=False)

Convert window coordinates to top-left coordinate space. The default origin is the top left corner of the window.

Args :

x_flipped (bool) - if the input x origin is flipped y_flipped (bool) - if the input y origin is flipped

Returns:

tuple (x, y) of converted window coordinates

If you are converting from bottom origin coordinates use x_flipped=True If you are converting from right origin coordinates use y_flipped=True

Window.print_context_info()

Prints moderngl context info.

Window Specific Methods

Window.on_mouse_press(x: int, y: int, button, mods)[source]

Handle mouse press events and forward to standard methods

Parameters:
  • x – x position of the mouse when pressed

  • y – y position of the mouse when pressed

  • button – The pressed button

  • mods – Modifiers

Window.on_key_release(symbol, modifiers)[source]

Pyglet specific key release callback.

Forwards and translates the events to standard methods.

Parameters:
  • symbol – The symbol of the pressed key

  • modifiers – Modifier state (shift, ctrl etc.)

Window.on_mouse_drag(x, y, dx, dy, buttons, modifiers)[source]

Pyglet specific mouse drag event.

When a mouse button is pressed this is the only way to capture mouse position events

Window.on_key_press(symbol, modifiers)[source]

Pyglet specific key press callback.

Forwards and translates the events to the standard methods.

Parameters:
  • symbol – The symbol of the pressed key

  • modifiers – Modifier state (shift, ctrl etc.)

Window.on_mouse_release(x: int, y: int, button, mods)[source]

Handle mouse release events and forward to standard methods

Parameters:
  • x – x position when mouse button was released

  • y – y position when mouse button was released

  • button – The button pressed

  • mods – Modifiers

Window.on_mouse_motion(x, y, dx, dy)[source]

Pyglet specific mouse motion callback.

Forwards and translates the event to the standard methods.

Parameters:
  • x – x position of the mouse

  • y – y position of the mouse

  • dx – delta x position

  • dy – delta y position of the mouse

Window.on_mouse_scroll(x, y, x_offset: float, y_offset: float)[source]

Handle mouse wheel.

Parameters:
  • x_offset (float) – X scroll offset

  • y_offset (float) – Y scroll offset

Window.on_text(text)[source]

Pyglet specific text input callback

Forwards and translates the events to the standard methods.

Parameters:

text (str) – The unicode character entered

Window.on_resize(width: int, height: int)[source]

Pyglet specific callback for window resize events forwarding to standard methods

Parameters:
  • width – New window width

  • height – New window height

Window.on_show()[source]

Called when window first appear or restored from hidden state

Window.on_hide()[source]

Called when window is minimized

Window.on_close()[source]

Pyglet specific window close callback

Window.on_file_drop(x, y, paths)[source]

Called when files dropped onto the window

Parameters:
  • x (int) – X location in window where file was dropped

  • y (int) – Y location in window where file was dropped

  • paths (list) – List of file paths dropped

Attributes

Window.name = 'pyglet'

Name of the window

Window.keys

Pyglet specific key constants

Window.ctx

The ModernGL context for the window

Type:

moderngl.Context

Window.backend

Name of the context backend.

This is None unless a backend is explicitly specified during context creation. The main use case for this is to enable EGL in headless mode.

Window.headless

Is the window headless?

Type:

bool

Window.fbo

The default framebuffer

Type:

moderngl.Framebuffer

Window.title

Window title.

This property can also be set:

window.title = "New Title"
Type:

str

Window.exit_key

Get or set the exit/close key for the window.

Pressing this key will close the window.

By default the ESCAPE is set, but this can be overridden or disabled:

# Default exit key
window.exit_key = window.keys.ESCAPE

# Set some other random exit key
window.exit_key = window.keys.Q

# Disable the exit key
window.exit_key = None
Window.fullscreen_key

Get or set the fullscreen toggle key for the window.

Pressing this key will toggle fullscreen for the window.

By default this is set to F11, but this can be overridden or disabled:

# Default fullscreen key
window.fullscreen_key = window.keys.F11

# Set some other random fullscreen key
window.fullscreen_key = window.keys.F

# Disable the fullscreen key
window.fullscreen_key = None
Window.gl_version

(major, minor) required OpenGL version

Type:

Tuple[int, int]

Window.width

The current window width

Type:

int

Window.height

The current window height

Type:

int

Window.size

current window size.

This property also support assignment:

# Resize the window to 1000 x 1000
window.size = 1000, 1000
Type:

Tuple[int, int]

Window.position

The current window position.

This property can also be set to move the window:

# Move window to 100, 100
window.position = 100, 100
Type:

Tuple[int, int]

Window.fullscreen

Window is in fullscreen mode

Type:

bool

Window.buffer_width

the current window buffer width

Type:

int

Window.buffer_height

the current window buffer height

Type:

int

Window.buffer_size

tuple with the current window buffer size

Type:

Tuple[int, int]

Window.pixel_ratio

The framebuffer/window size ratio

Type:

float

Window.viewport

current window viewport

Type:

Tuple[int, int, int, int]

Window.viewport_size

Size of the viewport.

Equivalent to self.viewport[2], self.viewport[3]

Type:

Tuple[int,int]

Window.viewport_width

The width of the viewport.

Equivalent to self.viewport[2].

Type:

int

Window.viewport_height

The height of the viewport

Equivalent to self.viewport[3].

Type:

int

Window.frames

Number of frames rendered

Type:

int

Window.resizable

Window is resizable

Type:

bool

Window.fullscreen

Window is in fullscreen mode

Type:

bool

Window.config

Get the current WindowConfig instance

DEPRECATED PROPERTY. This is not handled in WindowConfig.__init__

This property can also be set. Assinging a WindowConfig instance will automatically set up the necessary event callback methods:

window.config = window_config_instance
Window.vsync

vertical sync enabled/disabled

Type:

bool

Window.aspect_ratio

The current aspect ratio of the window. If a fixed aspect ratio was passed to the window initializer this value will always be returned. Otherwise width / height will be returned.

This property is read only.

Type:

float

Window.fixed_aspect_ratio

The fixed aspect ratio for the window.

Can be set to None to disable fixed aspect ratio making the aspect ratio adjust to the actual window size

This will affects how the viewport is calculated and the reported value from the aspect_ratio property:

# Enabled fixed aspect ratio
window.fixed_aspect_ratio = 16 / 9

# Disable fixed aspect ratio
window.fixed_aspect_ratio = None
Type:

float

Window.samples

Number of Multisample anti-aliasing (MSAA) samples

Type:

float

Window.cursor

Should the mouse cursor be visible inside the window?

This property can also be assigned to:

# Disable cursor
window.cursor = False
Type:

bool

Window.mouse_exclusivity

If mouse exclusivity is enabled.

When you enable mouse-exclusive mode, the mouse cursor is no longer available. It is not merely hidden – no amount of mouse movement will make it leave your application. This is for example useful when you don’t want the mouse leaving the screen when rotating a 3d scene.

This property can also be set:

window.mouse_exclusivity = True
Type:

bool

Window.render_func

The render callable

This property can also be used to assign a callable.

Type:

callable

Window.resize_func

Get or set the resize callable

Type:

callable

Window.close_func

Get or set the close callable

Type:

callable

Window.iconify_func

Get or set ehe iconify/show/hide callable

Type:

callable

Window.key_event_func

Get or set the key_event callable

Type:

callable

Window.on_generic_event_func

Get or set the on_generic_event callable used to funnel all non-processed events

Type:

callable

Window.mouse_position_event_func

Get or set the mouse_position callable

Type:

callable

Window.mouse_press_event_func

Get or set the mouse_press callable

Type:

callable

Window.mouse_release_event_func

Get or set the mouse_release callable

Type:

callable

Window.mouse_drag_event_func

Get or set the mouse_drag callable

Type:

callable

Window.unicode_char_entered_func

Get or set the unicode_char_entered callable

Type:

callable

Window.mouse_scroll_event_func

Get or set the mouse_scroll_event calable

Type:

callable

Window.files_dropped_event_func

Get or set the files_dropped callable

Type:

callable

Window.is_closing

Check pyglet’s internal exit state

Window.mouse = <class 'moderngl_window.context.base.window.MouseButtons'>

Mouse button enum

Window.mouse_states

Mouse button state structure.

The current mouse button states.

window.mouse_buttons.left
window.mouse_buttons.right
window.mouse_buttons.middle
Type:

MouseButtonStates

Window.modifiers

(KeyModifiers) The current keyboard modifiers

Window.gl_version_code

Generates the version code integer for the selected OpenGL version.

gl_version (4, 1) returns 410

Type:

int