Module albow.layout.Grid
Source code
from logging import Logger
from logging import getLogger
from pygame import Rect
from albow.core.ui.Widget import Widget
DEFAULT_GRID_COLUMN_SPACING = 10
DEFAULT_GRID_ROW_SPACING    = 10
class Grid(Widget):
    _is_gl_container = True
    margin = 0
    def __init__(self, rows, row_spacing=DEFAULT_GRID_ROW_SPACING, column_spacing=DEFAULT_GRID_COLUMN_SPACING, **kwds):
        self.logger: Logger = getLogger(__name__)
        self.margin = m = kwds.pop('margin', self.margin)
        col_widths  = [0] * len(rows[0])
        row_heights = [0] * len(rows)
        for j, row in enumerate(rows):
            for i, widget in enumerate(row):
                if widget:
                    col_widths[i]  = max(col_widths[i], widget.width)
                    row_heights[j] = max(row_heights[j], widget.height)
        self.logger.info(f"column_spacing: {column_spacing}")
        self.logger.info(f"... col_widths:   {col_widths} ... row_heights: {row_heights}")
        row_top:  int = 0
        col_left: int = 0
        for j, row in enumerate(rows):
            h = row_heights[j]
            y = m + row_top + h // 2
            col_left = 0
            for i, widget in enumerate(row):
                w = col_widths[i]
                if widget:
                    x = m + col_left
                    self.logger.info(f"x: {x} m: {m} + col_left: {col_left}")
                    widget.midleft = (x, y)
                col_left += w + column_spacing
            row_top += h + row_spacing
        #
        # Bad warning on "local variable col_left might be referenced before assignment"
        #
        width  = max(1, col_left - column_spacing)
        height = max(1, row_top - row_spacing)
        m2 = 2 * m
        self.logger.info(f"width: {width} height: {height} m2: {m2}")
        r = Rect(0, 0, width + m2 + column_spacing, height + m2)
        self.logger.info(f"r = {r}")
        super().__init__(r, **kwds)
        self.add(rows)
    def __repr__(self):
        return self.__class__.__name__Classes
- class Grid (rows, row_spacing=10, column_spacing=10, **kwds)
- 
The Widget class is the base class for all widgets. A widget occupies a rectangular area of the PyGame screen to which all drawing in it is clipped, and it may receive mouse and keyboard events. A widget may also contain subwidgets. Note Due to a limitation of PyGame subsurfaces, a widget's rectangle must be entirely contained within that of its parent widget. An exception will occur if this is violated. - Reading the following attributes retrieves the corresponding values from the widget's rect.
- Assigning to them changes the size and position of the widget.
- 
Additionally, if the size of the widget is changed via these attributes, the size and position of its subwidgets is updated according to each subwidget's anchor attribute. left, right, top, bottom, width, height, size, topleft, topright, bottomleft, bottomright, midleft, midright, midtop, midbottom, center, centerx, centery 
 This does not happen if the rect is modified directly. Creates a new widget, initially without any parent. If a rect is given, it specifies the new widget's initial s ize and position relative to its parent. Args- rect
- A PyGame rectangle defining the portion of the parent widget's coordinate system occupied by the
 widget. Modifying this rectangle changes the widget's size and position. - **kwds
- Additional attributes specified as key-value pairs
 Source codeclass Grid(Widget): _is_gl_container = True margin = 0 def __init__(self, rows, row_spacing=DEFAULT_GRID_ROW_SPACING, column_spacing=DEFAULT_GRID_COLUMN_SPACING, **kwds): self.logger: Logger = getLogger(__name__) self.margin = m = kwds.pop('margin', self.margin) col_widths = [0] * len(rows[0]) row_heights = [0] * len(rows) for j, row in enumerate(rows): for i, widget in enumerate(row): if widget: col_widths[i] = max(col_widths[i], widget.width) row_heights[j] = max(row_heights[j], widget.height) self.logger.info(f"column_spacing: {column_spacing}") self.logger.info(f"... col_widths: {col_widths} ... row_heights: {row_heights}") row_top: int = 0 col_left: int = 0 for j, row in enumerate(rows): h = row_heights[j] y = m + row_top + h // 2 col_left = 0 for i, widget in enumerate(row): w = col_widths[i] if widget: x = m + col_left self.logger.info(f"x: {x} m: {m} + col_left: {col_left}") widget.midleft = (x, y) col_left += w + column_spacing row_top += h + row_spacing # # Bad warning on "local variable col_left might be referenced before assignment" # width = max(1, col_left - column_spacing) height = max(1, row_top - row_spacing) m2 = 2 * m self.logger.info(f"width: {width} height: {height} m2: {m2}") r = Rect(0, 0, width + m2 + column_spacing, height + m2) self.logger.info(f"r = {r}") super().__init__(r, **kwds) self.add(rows) def __repr__(self): return self.__class__.__name__AncestorsInherited members- Widget:- add
- add_anchor
- add_centered
- anchor
- attention_lost
- augment_mouse_event
- bg_color
- bg_image
- border_color
- border_width
- call_handler
- call_parent_handler
- defer_drawing
- dismiss
- draw
- draw_over
- fg_color
- focus
- focus_switch
- font
- get_cursor
- get_focus
- get_margin_rect
- get_root
- get_top_widget
- get_visible
- global_to_local
- has_focus
- inherited
- invalidate
- is_gl_container
- key_down
- key_up
- local_to_global
- margin
- menu_bar
- parent
- parent_resized
- present
- rect
- relative_mode
- remove
- remove_anchor
- resized
- scale_bg
- sel_color
- set_parent
- set_size_for_text
- tab_stop
- visible