pygame_gui Documentation

repository·main·Indexed 21 days ago

https://github.com/myremylar/pygame_gui

A GUI system designed specifically for pygame Community Edition (pygame-ce). It provides a framework for building user interfaces within pygame applications, featuring a wide array of UI elements such as buttons, text entry boxes, panels, and specialized dialogs. The library includes a comprehensive theming system with support for dynamic updates, class-based styling, and multiple color formats, as well as a robust event system for handling user interactions.

Tokens
37.5K
Snippets
94
Records
155
Agent score
61%

What's inside pygame_gui

  1. Overview of Pygame GUI features

    main

    Pygame GUI is a module designed for creating graphical user interfaces for games written in Pygame (compatible with Pygame 2 and Python 3).

    Key features include:

    • Theme-able UI elements: Use JSON theme files to modify colors, fonts, and appearance without changing code.
    • HTML-like text support: A subset of HTML is supported for word-wrapped text, allowing for bold styling and links within paragraphs.
    • Standard Widgets: Includes buttons, text entry, scroll bars, and drop-down menus.
    • Window Stack: Supports multiple moveable and correctly sorted windows.
    • Localization: Built-in support for localizing the GUI into different languages.
    • Performance: Uses premultiplied alpha blending for improved clarity with semi-transparent pixels and better performance.
  2. Use the pygame_gui.windows package for dialogs and windows

    main

    The pygame_gui.windows package provides several pre-built window and dialog modules to simplify common UI tasks. Instead of building complex window structures from scratch, you can use these specialized submodules to handle user interactions like file selection, color picking, and message confirmation.

    Available window submodules include:

    • ui_colour_picker_dialog: A dialog for selecting colors.
    • ui_confirmation_dialog: A dialog for asking the user to confirm an action (e.g., Yes/No).
    • ui_console_window: A window for displaying console-like text output.
    • ui_file_dialog: A dialog for selecting files or directories from the system.
    • ui_message_window: A window for displaying informational messages to the user.
  3. Explore the pygame_gui.core package

    main

    The pygame_gui.core package contains the fundamental building blocks of the library. It includes subpackages for handling drawable shapes, interfaces, and text, as well as various modules for managing the core lifecycle and appearance of UI elements.

    Key functional areas include:

    • Visuals & Appearance: Modules like ui_appearance_theme, ui_shadow, colour_gradient, and surface_cache manage how elements look and how resources are cached.
    • Layout & Organization: ui_container and layered_gui_group provide mechanisms for grouping and nesting UI elements.
    • Resource Management: resource_loaders and gui_font_pygame handle the loading of assets like fonts and images.
    • Core Logic: ui_element defines the base behavior for UI components, while ui_window_stack manages windowing logic.
  4. How element theming hierarchy and IDs work

    main

    To theme specific elements, add blocks to your JSON file at the same level as the defaults block. These blocks use IDs to target specific components.

    ID Hierarchy and Specificity

    Pygame GUI uses a hierarchy to determine which theme rule to apply. More specific IDs take precedence over general ones. The order of preference is:

    1. object_id: A unique ID assigned to a specific instance.
    2. class_id: An ID representing a class of objects.
    3. element_id: The general ID of the element type.

    Addressing Sub-elements

    Use a full stop (.) to join IDs when targeting sub-elements.

    • Example: text_box.vertical_scroll_bar targets the scroll bar inside a text box.

    Special ID Prefixes

    • @: Used for certain sub-elements (e.g., vertical_scroll_bar.@arrow_button).
    • #: Used for even more specific sub-elements (e.g., vertical_scroll_bar.#bottom_button).

    Precedence Example:

    • vertical_scroll_bar.#bottom_button (Highest)
    • vertical_scroll_bar.@arrow_button
    • vertical_scroll_bar.button
    • button (Lowest)
  5. Configure element shapes and miscellaneous properties

    main

    The misc block in a theme file allows you to control more advanced visual properties. For example, you can change the shape of an element and its border properties using keys like shape, shape_corner_radius, and border_width. Note that these values are often passed as strings in the JSON file.

    {
        "button":
        {
            "colours": {
                "normal_border": "White",
                "normal_bg": "SlateGray",
                "normal_text": "White"
            },
            "misc": {
                "shape": "rounded_rectangle",
                "shape_corner_radius": "10",
                "border_width": "2"
            }
        }
    }
  6. How theming 'prototype' blocks work

    main
    Version 0.5.0 introduced prototype blocks in theming files. These blocks allow you to define common styling data once and reuse it, reducing repetition. Theming parameter inheritance was also improved; for example, styling a button block will now automatically affect buttons inside windows unless those buttons have a more specific theming block defined.
  7. Understand the structure of GUI events

    main

    When interacting with UI elements, pygame_gui produces pygame.Event objects. These events follow a consistent structure that allows you to identify which element triggered the action and which specific object was involved.

    Every GUI event contains these core attributes:

    • type: A unique identifier for the event (e.g., 'pygame_gui.UI_BUTTON_PRESSED').
    • ui_element: The actual UI element instance that fired the event.
    • ui_object_id: A unique string ID assigned to the UI element (e.g., 'hud_window.#sell_button').

    Note that some specific event types may include additional data fields relevant to that specific interaction.

  8. Use Class IDs for group theming (v0.5.7+)

    main

    In addition to unique object_ids, pygame_gui now supports class_ids. When creating a UIElement, you can pass an ObjectID that contains both a unique object_id and a class_id.

    Elements sharing the same class_id can be styled together in a theme file using a theming block, similar to how you would theme a single object ID. This allows for organized, group-based styling. You can also load multiple theme files into a single UIManager to organize your theme data.

  9. Understand Horizontal and Vertical positioning

    main

    Pygame GUI elements are positioned on the x (horizontal) and y (vertical) axes, starting from 0 at the top-left corner.

    Elements use a relative_rect for positioning, which is always relative to their container. If no container is provided, elements are assigned the default 'root container' created by the UI Manager, which matches the window_resolution.

    Dynamic Sizing: For certain elements like UIButton and UILabel, you can specify a dynamic width or height by setting the corresponding value in the relative_rect to -1. This allows the final size to be determined by the text content provided during creation.

    # Example of a button with a fixed size
    button_layout_rect = pygame.Rect(30, 20, 100, 20)
    
    # Example of a button with dynamic width (set to -1)
    dynamic_button_rect = pygame.Rect(30, 20, -1, 20)