From 95c6346d267f183ef418eaf90c84fc90b550bed8 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Thu, 9 Nov 2023 17:15:30 +0100 Subject: [PATCH] Pygame: Made it possible to pass a function as position for an text button to automatically update the position. --- pg.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pg.py b/pg.py index 41edede..a0f822a 100644 --- a/pg.py +++ b/pg.py @@ -98,23 +98,24 @@ class Button: 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): - self.text_object = self.generate_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.function = function - self.text_color = text_color self.bg_color = bg_color - self.font = font self.padding = padding - self.callable_position = None if position is int: self.position = position else: - self.position = position() + self.position = position(size=self.size) self.callable_position = position self.background = self.generate_background()