Python Bindings for GLFW

2.10.0 · active · verified Fri Apr 10

glfw (pyGLFW) is a ctypes-based wrapper for GLFW3, a cross-platform library for OpenGL, OpenGL ES and Vulkan development. It simplifies window creation, context management, and input handling in Python, providing a near one-to-one mapping to the C API with Pythonic naming conventions. The current version is 2.10.0, and it maintains an active release cadence with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart initializes GLFW, creates a basic window, sets up an OpenGL rendering context, and runs a simple event loop. It clears the window with a solid color until the user closes it. For actual rendering, you would integrate OpenGL (as shown with `OpenGL.GL`) or Vulkan commands within the main loop.

import glfw
import OpenGL.GL as gl

def main():
    # Initialize the library
    if not glfw.init():
        raise RuntimeError("Failed to initialize GLFW")

    # Create a windowed mode window and its OpenGL context
    window = glfw.create_window(640, 480, "Hello pyGLFW", None, None)
    if not window:
        glfw.terminate()
        raise RuntimeError("Failed to create GLFW window")

    # Make the window's context current
    glfw.make_context_current(window)

    # Set viewport and clear color
    gl.glViewport(0, 0, 640, 480)
    gl.glClearColor(0.2, 0.3, 0.4, 1.0)

    # Loop until the user closes the window
    while not glfw.window_should_close(window):
        # Render here
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        # Swap front and back buffers
        glfw.swap_buffers(window)

        # Poll for and process events
        glfw.poll_events()

    glfw.terminate()

if __name__ == "__main__":
    main()

view raw JSON →