Window based on pyglet.

Note that pylget is unable to make core 3.3+ contexts and will not work for certain drivers and enviroments such as on OS X.


Namespace mapping pyglet specific key constants



Opens a window using pyglet, registers input callbacks and creates a moderngl context.

Window.on_key_press(symbol, modifiers)

Pyglet specific key press callback. Forwards and translates the events to keyboard_event()

Window.on_key_release(symbol, modifiers)

Pyglet specific key release callback. Forwards and translates the events to keyboard_event()

Window.on_mouse_motion(x, y, dx, dy)

Pyglet specific mouse motion callback. Forwards and traslates the event to cursor_event()

Window.on_resize(width, height)

Pyglet specific callback for window resize events.


Render to this window


Swap buffers, increment frame counter and pull events

Window.should_close() → bool

returns the has_exit state in the pyglet window


Sets the close state in the pyglet window


No cleanup is really needed. Empty method

Window.resize(width, height)

Resize the window. Should normallty be overriden when implementing a window as most window libraries need additional logic here.

  • width (int) – Width of the window
  • height – (int): Height of the window

Other Inherited Methods

Window.draw(current_time, frame_time)

Draws a frame. Internally it calls the configured timeline’s draw method.

  • current_time (float) – The current time (preferrably always from the configured timer class)
  • frame_time (float) – The duration of the previous frame in seconds

Clear the window buffer

Window.clear_values(red=0.0, green=0.0, blue=0.0, alpha=0.0, depth=1.0)

Sets the clear values for the window buffer.

  • red (float) – red compoent
  • green (float) – green compoent
  • blue (float) – blue compoent
  • alpha (float) – alpha compoent
  • depth (float) – depth value
Window.keyboard_event(key, action, modifier)

Handles the standard keyboard events such as camera movements, taking a screenshot, closing the window etc.

Can be overriden add new keyboard events. Ensure this method is also called if you want to keep the standard features.

  • key – The key that was pressed or released
  • action – The key action. Can be ACTION_PRESS or ACTION_RELEASE
  • modifier – Modifiers such as holding shift or ctrl
Window.cursor_event(x, y, dx, dy)

The standard mouse movement event method. Can be overriden to add new functionality. By default this feeds the system camera with new values.

  • x – The current mouse x position
  • y – The current mouse y position
  • dx – Delta x postion (x position difference from the previous event)
  • dy – Delta y postion (y position difference from the previous event)

Prints moderngl context info.


Calculates the viewport based on the configured aspect ratio in settings. Will add black borders if the window do not match the viewport.



(width, height) tuple containing the window size.

Note that for certain displays we rely on buffer_size() to get the actual window buffer size. This is fairly common for retina and 4k displays where the UI scale is > 1.0


(width, heigh) buffer size of the window.

This is the actual buffer size of the window taking UI scale into account. A 1920 x 1080 window running in an environment with UI scale 2.0 would have a 3840 x 2160 window buffer.

Window.keys = <class 'demosys.context.pyglet.keys.Keys'>