World > Toolbar#

Toolbars are regular worlds that are usually docked beside the main world with world.camera.add_right(toolbar) or world.camera.add_bottom(toolbar).

They are especially useful together with buttons, because the active world can react to toolbar actions in on_message.

API Reference#

class miniworlds.worlds.gui.toolbar.Toolbar(*args, **kwargs)[source]#

A panel that holds buttons, labels, and other widgets.

Toolbars are usually docked beside the main world using world.camera.add_right(toolbar) or world.camera.add_bottom(toolbar). Widgets are added with toolbar.add(widget) (or by simply creating them - they register themselves automatically when their world is set to the toolbar).

When a button inside a toolbar is clicked it broadcasts its label text as a message. The main world responds via on_message.

Examples

toolbar = Toolbar()
toolbar.add(Button("Restart"))
world.camera.add_right(toolbar)

@world.register
def on_message(self, message):
    if message == "Restart":
        world.reset()
__init__()[source]#

Create a toolbar.

Examples

toolbar = Toolbar()
toolbar.add(Button("Start Rocket"))
world.camera.add_right(toolbar)
rocket.started = False

@world.register
def on_message(self, message):
    if message == "Start Rocket":
        rocket.started = True

@rocket.register
def act(self):
    if self.started:
        self.move()
property background_color#

Toolbar background color.

Examples

toolbar.background_color = (255, 255, 255)
Type:

tuple

can_scroll_down(value)[source]#

Returns True if the toolbar can scroll downward by value pixels.

Parameters:

value – Number of pixels to scroll by.

Returns:

True if scrolling is possible, False otherwise.

Return type:

bool

can_scroll_up(value)[source]#

Returns True if the toolbar can scroll upward by value pixels.

Parameters:

value – Number of pixels to scroll by.

Returns:

True if upward scrolling is possible, otherwise False.

Return type:

bool

dialog#
get_widget(key)[source]#

Gets widget by key

Return type:

BaseWidget

Parameters:

key – The key of the widget.

Returns:

The widget stored under key.

Raises:

TypeError – If no widget with the given key exists.

has_widget(key)[source]#

Checks whether the toolbar contains a widget under key.

Parameters:

key – The widget key.

Returns:

True if the widget exists, otherwise False.

Return type:

bool

on_change()[source]#

Reflows all widgets after the toolbar size or layout has changed.

This keeps widgets aligned when the toolbar is resized or docked in a different place.

on_new_actor(actor)[source]#

Handles actors that are added to this toolbar world.

Widget actors are registered in the internal widget list and then resized/reordered so the layout stays consistent.

Parameters:

actor – The actor that has been added.

on_remove_actor(actor)[source]#

Handles actors that are removed from this toolbar world.

Parameters:

actor – The actor that has been removed.

property padding_bottom#

Defines bottom margin

property padding_left#

Defines left margin

property padding_right#

Defines right margin

property padding_top#

Defines top margin

remove(item)[source]#

Removes a widget from the toolbar.

Warning

Be careful when calling this inside a loop over self.widgets.

Parameters:

item – The widget to remove – either the widget object itself, its integer index, or its string key.

remove_all_widgets()[source]#

Removes all widgets from the toolbar at once.

Use this when rebuilding a menu or replacing all controls.

reorder()[source]#

Recomputes the positions of all toolbar widgets.

Widgets are stacked from top to bottom with the configured paddings and margins.

scroll_down(value)[source]#

Scrolls the toolbar view downward by value pixels.

Parameters:

value – Number of pixels to scroll.

scroll_up(value)[source]#

Scrolls the toolbar view upward by value pixels.

Parameters:

value – Number of pixels to scroll.

send_message(text)[source]#

Sends a broadcast message to the world and all actors.

The message is dispatched through the event system and can be handled by any registered method in the world or its actors.

Return type:

None

Parameters:

text – The name of the message/event to send.

Example

>>> toolbar.send_message("open_menu")
update_width_and_height()[source]#

Updates cached toolbar dimensions after a layout change.

This hook is used internally when docking or resizing the toolbar.