CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-imgui

Cython-based Python bindings for dear imgui - a bloat-free immediate mode graphical user interface library

Pending
Overview
Eval results
Files

integrations.mddocs/

Integration Backends

Renderer integrations for various graphics libraries and frameworks including GLFW, SDL2, Pygame, Pyglet, and Cocos2D. These backends handle the platform-specific details of rendering ImGui draw commands and processing user input.

Capabilities

Base Classes

Foundation classes that provide common functionality for OpenGL-based renderers.

class BaseOpenGLRenderer:
    """Base class for OpenGL-based ImGui renderers."""
    
    def __init__(self) -> None:
        """Initialize the renderer with current ImGui context."""
    
    def render(self, draw_data) -> None:
        """Render ImGui draw data. Must be implemented by subclasses."""
    
    def refresh_font_texture(self) -> None:
        """Refresh font texture. Must be implemented by subclasses."""
    
    def shutdown(self) -> None:
        """Clean up renderer resources."""

OpenGL Renderers

Direct OpenGL integration for applications that manage their own OpenGL context.

class ProgrammablePipelineRenderer(BaseOpenGLRenderer):
    """Modern OpenGL renderer using programmable pipeline (OpenGL 3.0+)."""
    
    def __init__(self) -> None:
        """Initialize programmable pipeline renderer."""
    
    def render(self, draw_data) -> None:
        """Render using modern OpenGL shaders."""
    
    def refresh_font_texture(self) -> None:
        """Update font texture using modern OpenGL."""

class FixedPipelineRenderer(BaseOpenGLRenderer):
    """Legacy OpenGL renderer using fixed function pipeline (OpenGL 1.1)."""
    
    def __init__(self) -> None:
        """Initialize fixed pipeline renderer."""
    
    def render(self, draw_data) -> None:
        """Render using legacy OpenGL fixed pipeline."""
    
    def refresh_font_texture(self) -> None:
        """Update font texture using legacy OpenGL."""

GLFW Integration

Integration with GLFW for cross-platform window management and OpenGL context creation.

class GlfwRenderer(ProgrammablePipelineRenderer):
    """GLFW integration with ImGui using modern OpenGL."""
    
    def __init__(self, window) -> None:
        """Initialize with GLFW window object."""
    
    def process_inputs(self) -> None:
        """Process GLFW input events for ImGui."""
    
    def render(self, draw_data) -> None:
        """Render ImGui draw commands using OpenGL."""
    
    def keyboard_callback(self, window, key: int, scancode: int, action: int, mods: int) -> None:
        """Handle keyboard input callbacks."""
    
    def char_callback(self, window, char: int) -> None:
        """Handle character input callbacks."""
    
    def mouse_callback(self, window, x: float, y: float) -> None:
        """Handle mouse movement callbacks."""
    
    def scroll_callback(self, window, x_offset: float, y_offset: float) -> None:
        """Handle mouse scroll callbacks."""
    
    def resize_callback(self, window, width: int, height: int) -> None:
        """Handle window resize callbacks."""
    
    def shutdown(self) -> None:
        """Clean up GLFW-specific resources."""

SDL2 Integration

Integration with SDL2 for cross-platform multimedia and input handling.

class SDL2Renderer(ProgrammablePipelineRenderer):
    """SDL2 integration with ImGui using modern OpenGL."""
    
    def __init__(self, window) -> None:
        """Initialize with SDL2 window."""
    
    def process_inputs(self) -> None:
        """Process SDL2 input events for ImGui."""
    
    def render(self, draw_data) -> None:
        """Render ImGui draw commands using OpenGL."""
    
    def shutdown(self) -> None:
        """Clean up SDL2-specific resources."""

Pygame Integration

Integration with Pygame for game development and multimedia applications.

class PygameRenderer(FixedPipelineRenderer):
    """Pygame integration with ImGui using legacy OpenGL."""
    
    def __init__(self) -> None:
        """Initialize Pygame renderer."""
    
    def process_inputs(self) -> None:
        """Process Pygame input events for ImGui."""
    
    def render(self, draw_data) -> None:
        """Render ImGui draw commands using legacy OpenGL."""

Pyglet Integration

Integration with Pyglet for multimedia applications and game development.

class PygletRenderer(ProgrammablePipelineRenderer):
    """Pyglet integration with ImGui using modern OpenGL."""
    
    def __init__(self, window) -> None:
        """Initialize with Pyglet window."""
    
    def process_inputs(self) -> None:
        """Process Pyglet input events for ImGui."""

class PygletMixin:
    """Mixin class for Pyglet integration functionality."""

class PygletFixedPipelineRenderer(FixedPipelineRenderer, PygletMixin):
    """Pyglet renderer using fixed function pipeline."""

class PygletProgrammablePipelineRenderer(ProgrammablePipelineRenderer, PygletMixin):
    """Pyglet renderer using programmable pipeline."""

Cocos2D Integration

Integration with Cocos2D Python game development framework.

class ImguiLayer:
    """Cocos2D layer for ImGui integration."""
    
    def __init__(self) -> None:
        """Initialize ImGui layer for Cocos2D."""
    
    def on_enter(self) -> None:
        """Called when layer becomes active."""
    
    def on_exit(self) -> None:
        """Called when layer becomes inactive."""
    
    def draw(self) -> None:
        """Draw ImGui interface in Cocos2D."""

Glumpy Integration

Integration with Glumpy for scientific visualization applications.

class GlumpyRenderer(ProgrammablePipelineRenderer):
    """Glumpy integration with ImGui."""
    
    def __init__(self) -> None:
        """Initialize Glumpy renderer."""

Utility Functions

Helper functions for integration backends.

def compute_fb_scale(window_size: tuple[int, int], frame_buffer_size: tuple[int, int]) -> tuple[float, float]:
    """Compute framebuffer scale for high-DPI displays."""

Usage Examples

GLFW Integration

import glfw
import OpenGL.GL as gl
import imgui
from imgui.integrations.glfw import GlfwRenderer

def main():
    # Initialize GLFW
    if not glfw.init():
        return
    
    # Create window
    window = glfw.create_window(800, 600, "ImGui GLFW Example", None, None)
    if not window:
        glfw.terminate()
        return
    
    glfw.make_context_current(window)
    glfw.swap_interval(1)  # Enable vsync
    
    # Initialize ImGui
    imgui.create_context()
    impl = GlfwRenderer(window)
    
    # Main loop
    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        
        # Start new frame
        imgui.new_frame()
        
        # Create UI
        if imgui.begin("Hello, GLFW!"):
            imgui.text("This is rendered with GLFW backend")
            if imgui.button("Click me!"):
                print("Button clicked!")
        imgui.end()
        
        # Render
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)
    
    # Cleanup
    impl.shutdown()
    glfw.terminate()

if __name__ == "__main__":
    main()

SDL2 Integration

import sys
import sdl2
import OpenGL.GL as gl
import imgui
from imgui.integrations.sdl2 import SDL2Renderer

def main():
    # Initialize SDL2
    if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) < 0:
        return
    
    # Create window
    window = sdl2.SDL_CreateWindow(
        b"ImGui SDL2 Example",
        sdl2.SDL_WINDOWPOS_CENTERED,
        sdl2.SDL_WINDOWPOS_CENTERED,
        800, 600,
        sdl2.SDL_WINDOW_OPENGL | sdl2.SDL_WINDOW_RESIZABLE
    )
    
    # Create OpenGL context
    gl_context = sdl2.SDL_GL_CreateContext(window)
    sdl2.SDL_GL_SetSwapInterval(1)  # Enable vsync
    
    # Initialize ImGui
    imgui.create_context()
    impl = SDL2Renderer(window)
    
    # Main loop
    running = True
    while running:
        event = sdl2.SDL_Event()
        while sdl2.SDL_PollEvent(event):
            if event.type == sdl2.SDL_QUIT:
                running = False
        
        impl.process_inputs()
        
        # Start new frame
        imgui.new_frame()
        
        # Create UI
        if imgui.begin("Hello, SDL2!"):
            imgui.text("This is rendered with SDL2 backend")
            if imgui.button("Quit"):
                running = False
        imgui.end()
        
        # Render
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        imgui.render()
        impl.render(imgui.get_draw_data())
        sdl2.SDL_GL_SwapWindow(window)
    
    # Cleanup
    impl.shutdown()
    sdl2.SDL_GL_DeleteContext(gl_context)
    sdl2.SDL_DestroyWindow(window)
    sdl2.SDL_Quit()

if __name__ == "__main__":
    main()

Pygame Integration

import pygame
import OpenGL.GL as gl
import imgui
from imgui.integrations.pygame import PygameRenderer

def main():
    # Initialize Pygame
    pygame.init()
    size = (800, 600)
    pygame.display.set_mode(size, pygame.DOUBLEBUF | pygame.OPENGL)
    pygame.display.set_caption("ImGui Pygame Example")
    
    # Initialize ImGui
    imgui.create_context()
    impl = PygameRenderer()
    
    clock = pygame.time.Clock()
    running = True
    
    while running:
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        impl.process_inputs()
        
        # Start new frame
        imgui.new_frame()
        
        # Create UI
        if imgui.begin("Hello, Pygame!"):
            imgui.text("This is rendered with Pygame backend")
            fps = clock.get_fps()
            imgui.text(f"FPS: {fps:.1f}")
        imgui.end()
        
        # Render
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        imgui.render()
        impl.render(imgui.get_draw_data())
        pygame.display.flip()
        clock.tick(60)
    
    # Cleanup
    pygame.quit()

if __name__ == "__main__":
    main()

Pyglet Integration

import pyglet
import imgui
from imgui.integrations.pyglet import PygletRenderer

class ImGuiWindow(pyglet.window.Window):
    def __init__(self):
        super().__init__(800, 600, "ImGui Pyglet Example", resizable=True)
        
        # Initialize ImGui
        imgui.create_context()
        self.impl = PygletRenderer(self)
        
        # Schedule frame updates
        pyglet.clock.schedule_interval(self.update, 1/60.0)
    
    def on_draw(self):
        self.clear()
        
        # Start new frame
        imgui.new_frame()
        
        # Create UI
        if imgui.begin("Hello, Pyglet!"):
            imgui.text("This is rendered with Pyglet backend")
            if imgui.button("Toggle Fullscreen"):
                self.set_fullscreen(not self.fullscreen)
        imgui.end()
        
        # Render
        imgui.render()
        self.impl.render(imgui.get_draw_data())
    
    def update(self, dt):
        pass
    
    def on_close(self):
        self.impl.shutdown()
        super().on_close()

def main():
    window = ImGuiWindow()
    pyglet.app.run()

if __name__ == "__main__":
    main()

Basic Backend Selection

import imgui

# Choose backend based on availability
try:
    import glfw
    from imgui.integrations.glfw import GlfwRenderer
    print("Using GLFW backend")
except ImportError:
    try:
        import pygame
        from imgui.integrations.pygame import PygameRenderer
        print("Using Pygame backend")
    except ImportError:
        print("No supported backend found")
        sys.exit(1)

# Initialize ImGui context (required for all backends)
imgui.create_context()

Install with Tessl CLI

npx tessl i tessl/pypi-imgui

docs

drawing.md

index.md

input.md

integrations.md

layout.md

menus.md

styling.md

tables.md

tabs.md

widgets.md

windows.md

tile.json