A ctypes-based wrapper for GLFW3 that provides Python bindings for OpenGL, OpenGL ES, and Vulkan development on desktop platforms.
—
Core library initialization, termination, version queries, and error handling functions. These functions must be called in the correct order for proper GLFW operation.
Initialize the GLFW library before using any other GLFW functions. Must be called successfully before any other GLFW operations.
def init() -> int:
"""
Initialize the GLFW library.
Returns:
int: 1 (TRUE) if successful, 0 (FALSE) if failed
"""
def terminate() -> None:
"""
Terminate the GLFW library and free all resources.
This function destroys all remaining windows and cursors,
restores modified gamma ramps, and frees other resources.
"""Configure GLFW initialization behavior before calling init().
def init_hint(hint: int, value: int) -> None:
"""
Set initialization hints for GLFW library behavior.
Parameters:
hint: Initialization hint identifier
value: Hint value
"""# Platform selection hints
PLATFORM: int = 0x00050003
ANY_PLATFORM: int = 0x00060000
PLATFORM_WIN32: int = 0x00060001
PLATFORM_COCOA: int = 0x00060002
PLATFORM_WAYLAND: int = 0x00060003
PLATFORM_X11: int = 0x00060004
PLATFORM_NULL: int = 0x00060005
# macOS-specific hints
COCOA_CHDIR_RESOURCES: int = 0x00051001
COCOA_MENUBAR: int = 0x00051002
# X11-specific hints
X11_XCB_VULKAN_SURFACE: int = 0x00052001
# Wayland-specific hints
WAYLAND_LIBDECOR: int = 0x00053001
# Joystick behavior hints
JOYSTICK_HAT_BUTTONS: int = 0x00050001
# ANGLE platform hints
ANGLE_PLATFORM_TYPE: int = 0x00050002Query GLFW version and build information.
def get_version() -> tuple[int, int, int]:
"""
Get the GLFW library version.
Returns:
tuple: (major, minor, revision) version numbers
"""
def get_version_string() -> bytes:
"""
Get version string with compile-time configuration info.
Returns:
bytes: Version string describing build configuration
"""Retrieve and manage GLFW errors.
def get_error() -> tuple[int, bytes]:
"""
Get and clear the last error for the calling thread.
Returns:
tuple: (error_code, description) - error code and description
"""
def set_error_callback(cbfun) -> callable:
"""
Set the error callback function.
Parameters:
cbfun: Error callback function or None to remove callback
Signature: callback(error_code: int, description: str)
Returns:
callable: Previously set callback function or None
"""# Error code constants
NO_ERROR: int = 0
NOT_INITIALIZED: int = 0x00010001
NO_CURRENT_CONTEXT: int = 0x00010002
INVALID_ENUM: int = 0x00010003
INVALID_VALUE: int = 0x00010004
OUT_OF_MEMORY: int = 0x00010005
API_UNAVAILABLE: int = 0x00010006
VERSION_UNAVAILABLE: int = 0x00010007
PLATFORM_ERROR: int = 0x00010008
FORMAT_UNAVAILABLE: int = 0x00010009
NO_WINDOW_CONTEXT: int = 0x0001000A
CURSOR_UNAVAILABLE: int = 0x0001000B
FEATURE_UNAVAILABLE: int = 0x0001000C
FEATURE_UNIMPLEMENTED: int = 0x0001000D
PLATFORM_UNAVAILABLE: int = 0x0001000EQuery platform support and current platform.
def get_platform() -> int:
"""
Get the currently selected platform.
Returns:
int: Platform constant (PLATFORM_WIN32, PLATFORM_COCOA, etc.)
"""
def platform_supported(platform: int) -> int:
"""
Check if the library includes support for the specified platform.
Parameters:
platform: Platform constant to check
Returns:
int: 1 if supported, 0 if not supported
"""Configure custom memory allocation (advanced usage).
def init_allocator(allocate, reallocate, deallocate) -> None:
"""
Set custom memory allocator functions.
Parameters:
allocate: Custom allocation function or None
reallocate: Custom reallocation function or None
deallocate: Custom deallocation function or None
"""import glfw
# Initialize GLFW
if not glfw.init():
raise Exception("Failed to initialize GLFW")
try:
# Use GLFW functions here
pass
finally:
# Always terminate GLFW
glfw.terminate()import glfw
def error_callback(error_code, description):
print(f"GLFW Error {error_code}: {description.decode('utf-8')}")
# Set error callback before initialization
glfw.set_error_callback(error_callback)
if not glfw.init():
raise Exception("Failed to initialize GLFW")
# ... rest of application
glfw.terminate()import glfw
# Force X11 on Linux (instead of Wayland)
glfw.init_hint(glfw.PLATFORM, glfw.PLATFORM_X11)
if not glfw.init():
raise Exception("Failed to initialize GLFW")
glfw.terminate()Install with Tessl CLI
npx tessl i tessl/pypi-glfw