Text with Background

draw_text.add_text_with_background(text: str, x: int, y: int, margin_x: int, margin_y: int, color: int, bg_color: int, shadow: bool, display_duration: float, layer: int = 1) int

Add a text element with a background to the screen.

Parameters:
  • text – Text to display.

  • x – X-coordinate of the text position.

  • y – Y-coordinate of the text position.

  • margin_x – Horizontal padding between the text and the background box.

  • margin_y – Vertical padding between the text and the background box.

  • color – Text color. Must be created with argb(…) or taken from the Colors class.

  • bg_color – Background color. Must be created with argb(…) or taken from the Colors class.

  • shadow – Whether the text should be rendered with a shadow.

  • display_duration – How long the element remains on screen (in seconds).

  • layer – Rendering layer of the element. Higher layers appear above lower ones. Default is 1.

Returns:

ID of the created element.

draw_text.add_advanced_text_with_background(text: str, x: int, y: int, margin_x: int, margin_y: int, color: int, bg_color: int, shadow: bool, display_duration: float, layer: int, matrix: Matrix) int

Add a text element with a background to the screen, with additional scaling, rotation, and translation options.

Advanced version of add_text_with_background() that allows custom transformations.

Parameters:
  • text – Text to display.

  • x – X-coordinate of the text position.

  • y – Y-coordinate of the text position.

  • margin_x – Horizontal padding between the text and the background box.

  • margin_y – Vertical padding between the text and the background box.

  • color – Text color. Must be created with argb(…) or taken from the Colors class.

  • bg_color – Background color. Must be created with argb(…) or taken from the Colors class.

  • shadow – Whether the text should be rendered with a shadow.

  • display_duration – How long the element remains on screen (in seconds).

  • layer – Rendering layer of the element. Higher layers appear above lower ones.

  • matrix – Transformation matrix applied to the element (scale, rotation, translation). See Matrix.

Returns:

ID of the created element.

draw_text.animate_text_with_background(_id: int, func: Callable[[TextWithBackgroundObject], None]) None

Animates the text with background element with the given id by calling the given function every frame with the text with background element as the argument.

Parameters:
  • _id – ID of the text element to animate.

  • func – Function to use to animate the text element. The function is called every frame with the text with background element as the argument.

draw_text.modify_text_with_background(_id: int, func: Callable[[TextWithBackgroundObject], None]) None

Modifies the text with background element with the given id by calling the given function once with the text with background element as the argument.

Parameters:
  • _id – ID of the text element to modify.

  • func – Function to use to modify the text element. The function is called on the closest render frame.


TextWithBackgroundObject class

class draw_text.TextWithBackgroundObject(_id: int)

This class represents a text with background element. To create a TextWithBackgroundObject object, call the constructor with the id of the text with background element.

text: str

Text of the text element.

x: int

X-coordinate of the text.

y: int

Y-coordinate of the text.

margin_x: int

Horizontal padding between the text and the background box.

margin_y: int

Vertical padding between the text and the background box.

color: int

Text color. Must be created by the [argb]() function or from the [Colors]() class.

bg_color: int

Background color. Must be created with argb(…) or taken from the Colors class.

shadow: bool

True if the text has a shadow False otherwise.

display_duration_modifier: float

Modifier to the display_duration property.

layer: int

Layer of the element.

matrix: Matrix

Transformation matrix applied to the element (scale, rotation, translation).

property width

Width of the text. This property cannot be assigned.

property height

Height of the text. This property cannot be assigned.

property display_duration

Time in seconds that the text will remain on screen. This property cannot be assigned, use display_duration_modifier to change this value.

update(_id: int)

Updates this TextObject with the values of the text element specified by _id.

Parameters:

_id – ID of the text element.

to_list() list

Returns a list containing all the values of this TextWithBackgroundObject.

Returns:

List containing all the values of this TextWithBackgroundObject.


Example

 1from draw_text import *
 2
 3bg=argb(150,50,50,50)
 4
 5add_text_with_background("Hello, I have a background!", x=10, y=10, color=Colors.WHITE, bg_color=bg, shadow=True, display_duration=5, layer=1)
 6add_text_with_background("Above Hello!", x=10, y=20, color=Colors.WHITE, bg_color=bg, shadow=True, display_duration=5, layer=2)
 7
 8add_advanced_text_with_background("I am BIG!", x=10, y=50, color=Colors.WHITE, bg_color=bg, shadow=True, display_duration=5, layer=1, matrix=Matrix().scale(2))
 9
10text_id = add_text_with_background("I change colors!", x=10, y=80, color=Colors.RED, bg_color=bg, shadow=False, display_duration=5)
11
12animate_text_with_background(text_id, rainbow_animation) # Uses built-in rainbow animation