Actor > Widgets#

Use Button when a widget should simply send a message.

API Reference#

Button#

class miniworlds.actors.widgets.button.Button(text='', image='')[source]#

Clickable button widget for use inside a Toolbar.

When clicked, the button broadcasts its label text as a message to the world. The world or any actor can react with on_message.

Parameters:
  • text – The label text shown on the button. This same text is sent as a message when the button is clicked.

  • image – Optional path to an image shown on the button.

Examples

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

@world.register
def on_message(self, message):
    if message == "Start":
        print("Game started")
__init__(text='', image='')[source]#

Create a toolbar button.

Parameters:
  • text – The button label and the message that will be sent on click.

  • image – Optional image path for the button.

act()[source]#

Counts down the short click cooldown.

The cooldown prevents the button from sending the same message several times in a few frames while the mouse button is still held down.

Return type:

None

on_clicked_left(mouse_pos)[source]#

Called when the button is clicked.

By default, a message with the button text is then sent to the world.

Return type:

None

Examples

button = Button("Start Rocket")

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

Label#

class miniworlds.actors.widgets.label.Label(position: Tuple[float, float] | None = (0, 0), *args, **kwargs)[source]#

Text label for use in a Toolbar.

Label is usually used for headings, status text, scores, or other information that should be shown inside a toolbar.

Parameters:
  • text – The text to display in the label.

  • image – Optional path to an image to show instead of (or alongside) text.

Examples

toolbar = Toolbar()
score_label = Label("Score: 0")
toolbar.add(score_label)
world.camera.add_right(toolbar)
__init__(text, image=None)[source]#

Create a label widget.

Parameters:
  • text – The text displayed by the label.

  • image – Optional image path.

Input#

class miniworlds.actors.widgets.input.Input(position: Tuple[float, float] | None = (0, 0), *args, **kwargs)[source]#
property cursor_position#
has_focus()[source]#
property max_chars#
on_focus()[source]#
on_focus_lost()[source]#
on_key_down(keys)[source]#

Called once when a key is pressed.

Register on_key_down_<letter> (for example on_key_down_a) if you want to react to a specific letter only.

For arrow keys use on_key_down_left, on_key_down_right, on_key_down_up, or on_key_down_down.

Parameters:

key – List of key name variants, for example [“A”, “a”] or [“left”].

Examples

@player.register
def on_key_down(self, key):
    if "left" in key:
        self.direction = "left"
    elif "right" in key:
        self.direction = "right"
    self.move()

@player.register
def on_key_down_space(self):
    self.send_message("jump")
resize()[source]#
set_text(text)[source]#

Sets text of widget.

int and float values are converted to string.

update_cursor()[source]#

YesNoButton#

class miniworlds.actors.widgets.yesno.YesNoButton(position: Tuple[float, float] | None = (0, 0), *args, **kwargs)[source]#
get_no_button()[source]#
get_yes_button()[source]#

CounterLabel#

class miniworlds.actors.widgets.counter.CounterLabel(position: Tuple[float, float] | None = (0, 0), *args, **kwargs)[source]#

A counter label contains a description and a counter.

The counter starts with value 0 and can be modified with add and sub.

add(value)[source]#

Increase the counter by value.

get_value()[source]#
set(value)[source]#
sub(value)[source]#

Decrease the counter by value.

update_text()[source]#