Pygame: Made it possible to pass a function as position for an text button to automatically update the position.

This commit is contained in:
The Wobbler 2023-11-09 17:15:30 +01:00
parent 57ded2ff83
commit 95c6346d26

13
pg.py
View file

@ -98,23 +98,24 @@ class Button:
class TextButton: class TextButton:
def __init__(self, text: str, position: Union[tuple, Callable], surface: pygame.Surface, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: int=8): def __init__(self, text: str, position: Union[tuple, Callable], surface: pygame.Surface, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: int=8):
self.text_object = self.generate_text(text)
self.text = text self.text = text
self.text_color = text_color
self.font = font
self.text_object = self.generate_text(text)
self.size = (self.text_object.get_width() + padding * 2, self.text_object.get_height() + padding * 2)
self.surface = surface self.surface = surface
self.function = function self.function = function
self.text_color = text_color
self.bg_color = bg_color self.bg_color = bg_color
self.font = font
self.padding = padding self.padding = padding
self.callable_position = None self.callable_position = None
if position is int: if position is int:
self.position = position self.position = position
else: else:
self.position = position() self.position = position(size=self.size)
self.callable_position = position self.callable_position = position
self.background = self.generate_background() self.background = self.generate_background()