Window Guide

We support the following window types:

  • pyglet

  • glfw

  • sdl2

  • pygame2

  • pyside2

  • pyqt5

  • tk

  • headless

Using built in window types

The library provides shortcuts for window creation in the moderngl_window module that will also handle context activation.

The moderngl_window.conf.Settings instance has sane default parameters for a window. See the WINDOW attribute.

import moderngl_window
from moderngl_window.conf import settings

settings.WINDOW['class'] = 'moderngl_window.context.glfw.Window'
settings.WINDOW['gl_version'] = (4, 1)
# ... etc ...

# Creates the window instance and activates its context
window = moderngl_window.create_window_from_settings()

There are more sane ways to apply different configuration values through convenient methods in the Settings class.

Window classes can of course also be instantiated manually if preferred, but this can generate a bit of extra work.

import moderngl_window

window_str = 'moderngl_window.context.pyglet.Window'
window_cls = moderngl_window.get_window_cls(window_str)
window = window_cls(
    title="My Window",
    gl_version=(4, 1),
    size=(1920, 1080),

You could also simply import the class directory and instantiate it, but that defeats the purpose of trying to be independent of a specific window library.

The rendering loop for built in windows is simple:

while not window.is_closing:
    # Render stuff here

The swap_buffers method is important as it also pulls new input events for the next frame.

Old Guide

When not using a WindowConfig instance, there are a few simple steps to get started.

Register the moderngl.Context

When not using the built in window types you need to at least tell moderngl_window what your moderngl.Context is.

import moderngl
import moderngl_window

# Somewhere in your application a standalone or normal context is created
ctx = moderngl.create_standalone_context(require=330)
ctx = moderngl.create_context(require=330)

# Make sure you activate this context

If there is no context activated the library will raise an exception when doing operations that requires one, such as texture and scene loading.

When using the built in window types the context activation is normally done for you on creation.

Register resource directories

The resource loading system uses relative paths. These paths are relative to one or multiple directories we registered in the resource system.

The moderngl_window.resources module has methods for this.

from pathlib import Path
from moderngl_window import resources

# We recommend using pathlib
# .. but strings also works

These need to be absolute paths or an exception is raised. You can register as many paths as you want. The resource system will simply look for the file in every registered directory in the order they were added until it finds a match.

This library also supports separate search directories for shader programs, textures, scenes and various data files.