Button

class pygamepopup.components.Button(*args, **kwargs)

This class is representing any kind of button that could be seen on an interface.

A button is receptive to user clicks and returns an id corresponding to a method, it may have specific arguments too.

Mouse motion is also handled: the button appearance can change according to current focus.

Keyword Arguments:
  • callback (Callable) – the reference to the function that should be call after a click.

  • size (tuple[int, int]) – the size of the button following the format “(width, height)”, defaults to BUTTON_SIZE.

  • title (str) – the text that should be displayed at the center of the element.

  • position (Position) – the position of the element on the screen.

  • background_path (str) – the path to the image corresponding to the sprite of the element.

  • background_hover_path (str) – the path to the image corresponding to the sprite of the element when it has the focus.

  • no_background (bool) – specify whether a background should be present or not, defaults to False.

  • margin (Margin) – a tuple containing the margins of the box, should be in the form “(top_margin, right_margin, bottom_margin, left_margin)”, defaults to (0, 0, 0, 0).

  • disabled (bool) – a boolean indicating if it is not possible to interact with the button, defaults to False.

  • font (pygame.font.Font) – the font that should be used to render the text content.

  • text_color (pygame.Color) – the color of the text content, defaults to value from configuration.

  • font_hover (pygame.font.Font) – the font that should be used to render the text content when the mouse is over the button.

  • text_hover_color (pygame.Color) – the color of the text content when the mouse is over the button, defaults to value from configuration.

  • complementary_text_lines (str) – the other text lines that should be displayed in addition of the title.

  • column_span (int) – the number of columns the element should span, defaults to 1.

callback

the reference to the function that should be call after a click.

Type:

Callable

sprite

the pygame Surface corresponding to the sprite of the element.

Type:

pygame.Surface

sprite_hover

the pygame Surface corresponding to the sprite of the element when it has the focus.

Type:

pygame.Surface

action_triggered()

Method that should be called after a click.

Returns:

the callback that should be executed, a callback doing nothing would be returned if the button is disabled.

Return type:

Callable

render_sprite(background_path, rendered_text_lines)

Compute the rendering of the button with the given background and text lines. If no background is provided, render the text on an empty surface.

Returns:

the generated surface.

Return type:

pygame.Surface

Keyword Arguments:
  • background_path (str) – the path to the image corresponding to the sprite of the button.

  • rendered_text_lines (Sequence[pygame.Surface]) – the sequence of text lines in order that should be clipped on the surface.

static render_text_lines(text_lines, text_color, font)

Compute the rendering of the given text.

Returns:

the rendered text lines.

Return type:

Sequence[pygame.Surface]

Keyword Arguments:
  • text_lines (Sequence[str]) – the sequence in order of text lines to be rendered.

  • text_color (pygame.Color) – the color of the text.

  • font (pygame.font.Font) – the font that should be used to render the text.

set_hover(is_mouse_hover)

Change the current sprite between sprite or sprite_hover depending on whether the mouse is over the element or not.

Keyword Arguments:

is_mouse_hover (bool) – a boolean value indicating if the mouse is over the element or not