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)orworld.camera.add_bottom(toolbar). Widgets are added withtoolbar.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
from miniworlds import * world = World() toolbar = Toolbar() toolbar.add(Button("Restart")) world.camera.add_right(toolbar) @world.register def on_message(self, message): if message == "Restart": world.reset() world.run()
Public Data Attributes:
Background color as Tuple, e.g. (255,255,255) for white.
Defines left margin
Defines right margin
Defines top margin
Defines bottom margin
Inherited from
GUIis_runningevent_managerInherited from
WorldBackward-compatible docking API for older example code.
How often world logic runs relative to the frame loop.
Frames per second of the render loop.
Gets the horizontal size of the world in pixels.
Gets the vertical size of the world in pixels.
Gets the number of horizontal pixels (columns) visible in the world.
Gets the number of vertical pixels (rows) visible in the world.
Gets the world size as a tuple (width, height), in pixels.
Returns the currently active background.
Returns True if the world has at least one background appearance.
is_runningevent_managerInherited from
WorldBasewindowsizetopleftwidthheightclass_nameregistered_eventsReturns the set of all event names that are currently registered.
is_runningevent_managerPublic Methods:
__init__()Base class for toolbars.
Reflows all widgets after the toolbar size or layout has changed.
remove(item)Removes a widget from the toolbar.
has_widget(key)Checks whether the toolbar contains a widget under key.
get_widget(key)Gets widget by key
Removes all widgets from the toolbar at once.
reorder()Recomputes the positions of all toolbar widgets.
Updates cached toolbar dimensions after a layout change.
send_message(text)Sends a broadcast message to the world and all actors.
scroll_up(value)Scrolls the toolbar view upward by value pixels.
scroll_down(value)Scrolls the toolbar view downward by value pixels.
can_scroll_down(value)Returns True if the toolbar can scroll downward by value pixels.
can_scroll_up(value)Returns True if the toolbar can scroll upward by value pixels.
on_new_actor(actor)Handles actors that are added to this toolbar world.
on_remove_actor(actor)Handles actors that are removed from this toolbar world.
Inherited from
GUIadd(actor)Inherited from
World__init__([x, y])Initializes the world and all internal managers needed for runtime operation.
contains_position(pos)Checks if position is in the world.
contains_rect(rect)Returns True if the entire rectangle is fully inside the world.
contains_rect_any(rect)Returns True if any part of the rectangle is inside the world.
set_columns(value)Internal method to set columns and sync world width.
set_rows(value)Internal method to set rows and sync world height.
Returns the current active background from the backgrounds manager.
switch_background(background)Switches the current background to a specified one.
remove_background([background])Removes a background from the world.
set_background(source)Sets a new background and replaces the current active background.
add_background(source)Adds a new background to the world and sets it as the active one.
start()Starts or resumes the world.
stop([frames])Stops the world immediately or after a delay in frames.
run([fullscreen, fit_desktop, replit, ...])Starts the main application loop of the Miniworlds engine.
is_in_world(position)Checks whether a given world position lies within the world's boundaries.
send_message(message[, data])Sends a broadcast message to the world and all actors.
switch_world(new_world[, reset])Switch the active scene to another world.
load_world_from_db(file)Load a saved world from a sqlite database file and activate it.
load_actors_from_db(file, actor_classes)Load actors from a sqlite database file into the current world.
save_to_db(file)Save the current world and its actors to a sqlite database file.
quit([exit_code])Immediately quits the application and closes the game window.
reset()Resets the world Creates a new world with init-function - recreates all actors and actors on the world.
get_from_pixel(position)Converts a screen pixel position into a valid world position if inside bounds.
to_pixel(position)Converts a world position to a screen pixel position.
on_setup()Hook method to define initial setup logic when the world is created.
detect_actors(position)Gets all actors which are found at a specific position (in global world coordinates)
get_actors_from_pixel(pixel)Returns a list of all actors located at the given screen pixel position.
distance_to(pos1, pos2)Calculates the Euclidean distance between two positions.
direction_to(pos1, pos2)Calculates the angle from pos1 to pos2 in degrees.
Inherited from
WorldBase__init__()remove(actor)Implemented in subclasses
on_change()implemented in subclasses
on_new_actor(actor)on_remove_actor(actor)get_world_connector(actor)screenshot([filename])Saves a screenshot of the current window surface to a file.
get_events()Prints a list of all events that can be registered in this world.
register(method)Registers a method as a world event handler.
Private Data Attributes:
_abc_impl_default_start_runningInherited from
GUI_abc_impl_default_start_runningInherited from
World_abc_impl_default_start_runningInherited from
WorldBase_abc_impl_default_start_runningPrivate Methods:
_get_mainloopmanager_class()_add_widget(widget[, key])_widgets_total_height()_set_widget_width(widget)Inherited from
GUI_get_world_connector_class()needed by get_world_connector in parent class
Inherited from
World_validate_parameters(x, y)_get_initialization_facade()_get_background_facade()_get_runtime_facade()_clear()Clears the world's state: event queue, all backgrounds, and all actors.
Inherited from
WorldBase_after_init_setup()_get_mainloopmanager_class()_get_camera_manager_class()_get_world_connector_class()needed by get_world_connector in parent class
_create_event_manager()_unregister(method)Unregisters a previously registered world method.
_start_listening()Enables input listening for the world.
_stop_listening()Disables input listening for the world.
- __init__()[source]#
Base class for toolbars.
Example
Add a Toolbar which interacts with Actors on world via messages:
from miniworlds import * world = World() world.add_background("images/galaxy.jpg") toolbar = Toolbar() button = Button("Start Rocket") toolbar.add(button) world.camera.add_right(toolbar) @world.register def on_message(self, message): if message == "Start Rocket": rocket.started = True rocket = Actor(100, 200) rocket.add_costume("images/ship.png") rocket.started = False rocket.turn_left(90) rocket.direction = "up" @rocket.register def act(self): if self.started: self.move() @rocket.register def on_sensing_not_on_the_world(self): self.remove() world.run()
- property background_color#
Background color as Tuple, e.g. (255,255,255) for white
- 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:
- 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:
- event_manager: event_manager.EventManager#
- 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:
- is_running: 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:
- 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.
- widgets: OrderedDict['widget_base.BaseWidget']#