CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-glfw

A ctypes-based wrapper for GLFW3 that provides Python bindings for OpenGL, OpenGL ES, and Vulkan development on desktop platforms.

Pending
Overview
Eval results
Files

window-management.mddocs/

Window Management

Complete window lifecycle management including creation, destruction, properties, state control, and callbacks. Windows provide the rendering surface for OpenGL and Vulkan applications.

Capabilities

Window Creation and Destruction

Create and destroy windows with associated OpenGL or Vulkan contexts.

def create_window(width: int, height: int, title: str, monitor, share):
    """
    Create a window and its associated OpenGL or OpenGL ES context.
    
    Parameters:
        width: Window width in screen coordinates
        height: Window height in screen coordinates  
        title: Window title (UTF-8 encoded string)
        monitor: Monitor for fullscreen mode, or None for windowed
        share: Window whose context to share resources with, or None
        
    Returns:
        GLFWwindow: Window handle, or None if creation failed
    """

def destroy_window(window) -> None:
    """
    Destroy the window and its context.
    
    Parameters:
        window: Window to destroy
    """

Window Hints

Configure window properties before creation.

def default_window_hints() -> None:
    """Reset all window hints to their default values."""

def window_hint(hint: int, value: int) -> None:
    """
    Set window creation hint.
    
    Parameters:
        hint: Window hint identifier
        value: Hint value
    """

def window_hint_string(hint: int, value: str) -> None:
    """
    Set string window hint.
    
    Parameters:
        hint: Window hint identifier
        value: Hint string value
    """

Window Hint Constants

# Window properties
FOCUSED: int = 0x00020001
ICONIFIED: int = 0x00020002
RESIZABLE: int = 0x00020003
VISIBLE: int = 0x00020004
DECORATED: int = 0x00020005
AUTO_ICONIFY: int = 0x00020006
FLOATING: int = 0x00020007
MAXIMIZED: int = 0x00020008
CENTER_CURSOR: int = 0x00020009
TRANSPARENT_FRAMEBUFFER: int = 0x0002000A
HOVERED: int = 0x0002000B
FOCUS_ON_SHOW: int = 0x0002000C
MOUSE_PASSTHROUGH: int = 0x0002000D
POSITION_X: int = 0x0002000E
POSITION_Y: int = 0x0002000F

# Framebuffer properties
RED_BITS: int = 0x00021001
GREEN_BITS: int = 0x00021002
BLUE_BITS: int = 0x00021003
ALPHA_BITS: int = 0x00021004
DEPTH_BITS: int = 0x00021005
STENCIL_BITS: int = 0x00021006
SAMPLES: int = 0x0002100D
SRGB_CAPABLE: int = 0x0002100E
DOUBLEBUFFER: int = 0x00021010

# Context properties
CLIENT_API: int = 0x00022001
CONTEXT_VERSION_MAJOR: int = 0x00022002
CONTEXT_VERSION_MINOR: int = 0x00022003
CONTEXT_REVISION: int = 0x00022004
CONTEXT_ROBUSTNESS: int = 0x00022005
OPENGL_FORWARD_COMPAT: int = 0x00022006
OPENGL_DEBUG_CONTEXT: int = 0x00022007
OPENGL_PROFILE: int = 0x00022008
CONTEXT_RELEASE_BEHAVIOR: int = 0x00022009
CONTEXT_NO_ERROR: int = 0x0002200A
CONTEXT_CREATION_API: int = 0x0002200B

Window State

Query and control window state and lifecycle.

def window_should_close(window) -> int:
    """
    Check the close flag of the window.
    
    Parameters:
        window: Window to query
        
    Returns:
        int: 1 if window should close, 0 otherwise
    """

def set_window_should_close(window, value: int) -> None:
    """
    Set the close flag of the window.
    
    Parameters:
        window: Window to modify
        value: Close flag value (1 to close, 0 to keep open)
    """

def iconify_window(window) -> None:
    """Iconify (minimize) the window."""

def restore_window(window) -> None:
    """Restore the window from iconified/maximized state."""

def maximize_window(window) -> None:
    """Maximize the window."""

def show_window(window) -> None:
    """Make the window visible."""

def hide_window(window) -> None:
    """Hide the window."""

def focus_window(window) -> None:
    """Bring the window to front and set input focus."""

def request_window_attention(window) -> None:
    """Request user attention to the window."""

Window Properties

Get and set window title, position, size, and other properties.

def set_window_title(window, title: str) -> None:
    """Set the window title."""

def get_window_title(window) -> str:
    """Get the window title."""

def get_window_pos(window) -> tuple[int, int]:
    """
    Get the position of the window's client area.
    
    Returns:
        tuple: (x, y) position in screen coordinates
    """

def set_window_pos(window, xpos: int, ypos: int) -> None:
    """Set the position of the window's client area."""

def get_window_size(window) -> tuple[int, int]:
    """
    Get the size of the window's client area.
    
    Returns:
        tuple: (width, height) in screen coordinates
    """

def set_window_size(window, width: int, height: int) -> None:
    """Set the size of the window's client area."""

def get_framebuffer_size(window) -> tuple[int, int]:
    """
    Get the size of the framebuffer in pixels.
    
    Returns:
        tuple: (width, height) in pixels
    """

def get_window_frame_size(window) -> tuple[int, int, int, int]:
    """
    Get the size of the frame around the window.
    
    Returns:
        tuple: (left, top, right, bottom) frame sizes in screen coordinates
    """

def get_window_content_scale(window) -> tuple[float, float]:
    """
    Get the content scale for the window.
    
    Returns:
        tuple: (x_scale, y_scale) content scale factors
    """

Window Constraints

Set size limits and aspect ratio constraints.

def set_window_size_limits(window, minwidth: int, minheight: int, 
                          maxwidth: int, maxheight: int) -> None:
    """
    Set the size limits of the window.
    
    Parameters:
        minwidth: Minimum width, or DONT_CARE
        minheight: Minimum height, or DONT_CARE
        maxwidth: Maximum width, or DONT_CARE  
        maxheight: Maximum height, or DONT_CARE
    """

def set_window_aspect_ratio(window, numer: int, denom: int) -> None:
    """
    Set the aspect ratio of the window.
    
    Parameters:
        numer: Numerator of aspect ratio, or DONT_CARE
        denom: Denominator of aspect ratio
    """

Window Attributes

Query and modify window attributes.

def get_window_attrib(window, attrib: int) -> int:
    """
    Get a window attribute.
    
    Parameters:
        attrib: Attribute to query
        
    Returns:
        int: Attribute value
    """

def set_window_attrib(window, attrib: int, value: int) -> None:
    """
    Set a modifiable window attribute.
    
    Parameters:
        attrib: Attribute to set
        value: New attribute value
    """

def get_window_opacity(window) -> float:
    """
    Get the opacity of the window.
    
    Returns:
        float: Window opacity (0.0-1.0)
    """

def set_window_opacity(window, opacity: float) -> None:
    """
    Set the opacity of the window.
    
    Parameters:
        opacity: Opacity value (0.0-1.0)
    """

Window User Data

Associate custom data with windows.

def set_window_user_pointer(window, pointer) -> None:
    """
    Set the user pointer of the window.
    
    Parameters:
        pointer: User data (any Python object)
    """

def get_window_user_pointer(window):
    """
    Get the user pointer of the window.
    
    Returns:
        User data object previously set
    """

Window Icons

Set custom window icons.

def set_window_icon(window, count: int, images) -> None:
    """
    Set the icon of the window.
    
    Parameters:
        count: Number of images in the array
        images: Array of image data or PIL Image objects
    """

Window Monitor Association

Manage fullscreen and windowed mode.

def get_window_monitor(window):
    """
    Get the monitor that the window uses for fullscreen mode.
    
    Returns:
        GLFWmonitor: Monitor handle, or None if windowed
    """

def set_window_monitor(window, monitor, xpos: int, ypos: int, 
                      width: int, height: int, refresh_rate: int) -> None:
    """
    Set the mode, monitor, video mode and placement of a window.
    
    Used to switch between fullscreen and windowed mode or change
    the monitor and video mode of a fullscreen window.
    
    Parameters:
        monitor: Target monitor, or None for windowed mode
        xpos: Window x-position for windowed mode
        ypos: Window y-position for windowed mode  
        width: Window/video mode width
        height: Window/video mode height
        refresh_rate: Video mode refresh rate, or DONT_CARE
    """

Window Callbacks

Register callback functions for window events.

def set_window_pos_callback(window, cbfun) -> callable:
    """
    Set the window position callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(window, xpos: int, ypos: int)
    """

def set_window_size_callback(window, cbfun) -> callable:
    """
    Set the window size callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(window, width: int, height: int)
    """

def set_window_close_callback(window, cbfun) -> callable:
    """
    Set the window close callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(window)
    """

def set_window_refresh_callback(window, cbfun) -> callable:
    """
    Set the window refresh callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(window)
    """

def set_window_focus_callback(window, cbfun) -> callable:
    """
    Set the window focus callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(window, focused: int)
    """

def set_window_iconify_callback(window, cbfun) -> callable:
    """
    Set the window iconify callback.
    
    Parameters:
        cbfun: Callback function or None  
            Signature: callback(window, iconified: int)
    """

def set_window_maximize_callback(window, cbfun) -> callable:
    """
    Set the window maximize callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(window, maximized: int)
    """

def set_framebuffer_size_callback(window, cbfun) -> callable:
    """
    Set the framebuffer size callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(window, width: int, height: int)
    """

def set_window_content_scale_callback(window, cbfun) -> callable:
    """
    Set the window content scale callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(window, xscale: float, yscale: float)
    """

Usage Examples

Basic Window Creation

import glfw

# Initialize GLFW
glfw.init()

# Create a windowed mode window and its OpenGL context
window = glfw.create_window(640, 480, "Hello World", None, None)

if not window:
    glfw.terminate()
    raise Exception("Failed to create window")

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

# Main loop
while not glfw.window_should_close(window):
    glfw.poll_events()
    glfw.swap_buffers(window)

glfw.terminate()

Window with Modern OpenGL Context

import glfw

glfw.init()

# Request OpenGL 3.3 Core Profile
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, glfw.TRUE)  # macOS compatibility

window = glfw.create_window(800, 600, "Modern OpenGL", None, None)

if not window:
    glfw.terminate()
    raise Exception("Failed to create window")

glfw.make_context_current(window)
# ... rest of application

Fullscreen Window

import glfw

glfw.init()

# Get the primary monitor
monitor = glfw.get_primary_monitor()
mode = glfw.get_video_mode(monitor)

# Create fullscreen window
window = glfw.create_window(mode.size.width, mode.size.height, 
                           "Fullscreen", monitor, None)

Window Callbacks

import glfw

def window_size_callback(window, width, height):
    print(f"Window resized to {width}x{height}")

def window_close_callback(window):
    print("Window close requested")
    # Could prevent closing by not setting the close flag
    # glfw.set_window_should_close(window, False)

glfw.init()
window = glfw.create_window(640, 480, "Callbacks Example", None, None)

# Set callbacks
glfw.set_window_size_callback(window, window_size_callback)
glfw.set_window_close_callback(window, window_close_callback)

# Main loop...

Install with Tessl CLI

npx tessl i tessl/pypi-glfw

docs

index.md

input-handling.md

library-management.md

monitor-display.md

opengl-context.md

vulkan-support.md

window-management.md

tile.json