moderngl-window

3.1.1 · active · verified Thu Apr 16

moderngl-window is a cross-platform helper library for ModernGL, simplifying window creation and resource loading for OpenGL applications. It provides a unified API over various windowing backends like Pyglet, GLFW, and SDL2. The library is actively maintained with frequent updates, currently at version 3.1.1, ensuring compatibility and introducing new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a basic window by extending `moderngl_window.WindowConfig`. It sets up an OpenGL 3.3 context, specifies window dimensions and title, and provides a minimal `on_render` method to clear the screen with a red color. The `run()` method then starts the window's event loop.

import moderngl_window as mglw

class MyWindow(mglw.WindowConfig):
    # Set OpenGL version (e.g., 3.3 for core profile)
    gl_version = (3, 3)
    # Set window size
    window_size = (1280, 720)
    # Set window title
    title = "Hello, moderngl-window!"
    
    # Optionally, specify a resource directory for shaders, textures etc.
    # resource_dir = Path(__file__).parent.resolve() / "resources"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # Any setup for ModernGL context or resources goes here
        print("ModernGL context initialized!")

    def on_render(self, time: float, frametime: float):
        # Clear the framebuffer with a color (e.g., red)
        self.ctx.clear(1.0, 0.0, 0.0, 1.0)

    # You can also implement other event handlers like on_key_event, on_mouse_press_event etc.

if __name__ == '__main__':
    # Run the window config. This creates the window and starts the event loop.
    MyWindow.run()

view raw JSON →