BoxLayout

class pygamelib.gfx.ui.BoxLayout(orientation: Orientation | None = None, size_constraint: SizeConstraint | None = None, parent: Widget | None = None)

Bases: Layout

The box layout lines up child widgets horizontally or vertically. The orientation of the layout is controlled using the Orientation constants.

__init__(orientation: Orientation | None = None, size_constraint: SizeConstraint | None = None, parent: Widget | None = None) None
Parameters:
  • orientation (Orientation) – The orientation of the layout.

  • size_constraint (SizeConstraint) – The size constraint policy for managed widgets.

  • parent (Widget) – The parent object, ie: the one in which the GridLayout reside.

Example:

parent_widget = Widget(45, 30, config=config)
# Add a GridLayout to a widget
parent_widget.layout = BoxLayout(orientation=Orientation.VERTICAL)
parent_widget.layout.add_widget(LineInput())

Methods

__init__([orientation, size_constraint, parent])

param orientation:

The orientation of the layout.

add_widget(w)

Add a widget to the BoxLayout.

attach(observer)

Attach an observer to this instance.

count()

Returns the amount (the count) of widgets in the BoxLayout.

detach(observer)

Detach an observer from this instance.

handle_notification(subject[, attribute, value])

A virtual method that needs to be implemented by the observer.

notify([modifier, attribute, value])

Notify all the observers that a change occurred.

render_to_buffer(buffer, row, column, ...)

Render the object from the display buffer to the frame buffer.

store_screen_position(row, column)

Store the screen position of the object.

widgets()

Returns the list of widgets that are managed by the GridLayout as a set.

Attributes

height

Get the layout's height (including spacing).

orientation

Get and set the layout's orientation.

parent

This property get/set the parent of the Layout (if any).

screen_column

A property to get/set the screen column.

screen_row

A property to get/set the screen row.

size_constraint

Get and set the layout's size constraint policy.

spacing

This property get/set the inter-widgets spacing of the Layout.

width

Get the layout's width (including spacing).

add_widget(w: Widget) bool

Add a widget to the BoxLayout. If the widget is correctly added to the layout this method returns True, otherwise it returns False.

Parameters:

widget (class:Widget) – The widget to add to the layout.

Example:

parent_widget = Widget(45, 30, config=config)
# Add a BoxLayout to a widget
parent_widget.layout = BoxLayout()
# Then add children widgets to the parent's layout
# Step 1: create a widget (here just a generic widget)
child_widget1 = Widget(config=UiConfig.instance())
# Step 2: add it to the layout.
parent_widget.layout.add_widget(child_widget1)
# That's it!
attach(observer)

Attach an observer to this instance. It means that until it is detached, it will be notified every time that a notification is issued (usually on changes).

An object cannot add itself to the list of observers (to avoid infinite recursions).

Parameters:

observer (PglBaseObject) – An observer to attach to this object.

Returns:

True or False depending on the success of the operation.

Return type:

bool

Example:

myboard = Board()
screen = Game.instance().screen
# screen will be notified of all changes in myboard
myboard.attach(screen)
count() int

Returns the amount (the count) of widgets in the BoxLayout.

detach(observer)

Detach an observer from this instance. If observer is not in the list this returns False.

Parameters:

observer (PglBaseObject) – An observer to detach from this object.

Returns:

True or False depending on the success of the operation.

Return type:

bool

Example:

# screen will no longer be notified of the changes in myboard.
myboard.detach(screen)
handle_notification(subject, attribute=None, value=None)

A virtual method that needs to be implemented by the observer. By default it does nothing but each observer needs to implement it if something needs to be done when notified.

This method always receive the notifying object as first parameter. The 2 other parameters are optional and can be None.

You can use the attribute and value as you see fit. You are free to consider attribute as an event and value as the event’s value.

Parameters:
  • subject (PglBaseObject) – The object that has changed.

  • attribute (str) – The attribute that has changed, it is usually a “FQDN style” string. This can be None.

  • value (Any) – The new value of the attribute. This can be None.

property height: int

Get the layout’s height (including spacing).

Returns an int.

notify(modifier=None, attribute: str = None, value: Any = None) None

Notify all the observers that a change occurred.

Parameters:
  • modifier (PglBaseObject) – An optional parameter that identify the modifier object to exclude it from the notified objects.

  • attribute (str) – An optional parameter that identify the attribute that has changed.

  • value (Any) – An optional parameter that identify the new value of the attribute.

Example:

# This example is silly, you would usually notify other objects from inside
# an object that changes a value that's important for the observers.
color = Color(255,200,125)
color.attach(some_text_object)
color.notify()
property orientation: Orientation

Get and set the layout’s orientation.

The orientation of the BoxLayout can be changed dynamically and will take effect immediately.

It has to be a Orientation.

property parent: Widget | None

This property get/set the parent of the Layout (if any).

render_to_buffer(buffer: numpy.array, row: int, column: int, buffer_height: int, buffer_width: int) None

Render the object from the display buffer to the frame buffer.

This method is automatically called by pygamelib.engine.Screen.render().

Parameters:
  • buffer (numpy.array) – A screen buffer to render the item into.

  • row (int) – The row to render in.

  • column (int) – The column to render in.

  • height (int) – The total height of the display buffer.

  • width (int) – The total width of the display buffer.

property screen_column: int

A property to get/set the screen column.

Parameters:

value (int) – the screen column

Return type:

int

property screen_row: int

A property to get/set the screen row.

Parameters:

value (int) – the screen row

Return type:

int

property size_constraint: SizeConstraint

Get and set the layout’s size constraint policy. It has to be a SizeConstraint.

property spacing: int

This property get/set the inter-widgets spacing of the Layout.

When the spacing is changed the pygamelib.gfx.ui.Layout.spacing:changed event is sent to the observers.

store_screen_position(row: int, column: int) bool

Store the screen position of the object.

This method is automatically called by Screen.place().

Parameters:
  • row (int) – The row (or y) coordinate.

  • column (int) – The column (or x) coordinate.

Example:

an_object.store_screen_coordinate(3,8)
widgets() Set[Widget]

Returns the list of widgets that are managed by the GridLayout as a set. This set is not guaranteed to be ordered!

property width: int

Get the layout’s width (including spacing).

Returns an int.