Button¶
Buttons provide clickable interface elements with hover effects.
Basic Usage¶
button = pygameui.Button(
position=(100, 100),
label="Click me.",
width=200,
height=50
)
# In the main loop
button.update(events) # Pass events list for proper interaction handling
button.draw(screen)
# Check for clicks
if button.was_clicked():
print("Button was clicked!")
Properties¶
position: tuple[int, int],
width: int = 200,
height: int = 50,
border_radius: int = 10,
border_color: tuple[int, int, int] = None,
border_width: int = 2,
label: str = "Click me.",
color: tuple[int, int, int] = (255, 255, 255),
hover_color: tuple[int, int, int] = (200, 200, 200),
click_color: tuple[int, int, int] = (150, 150, 150),
text_color: tuple[int, int, int] = (100, 100, 100),
text_hover_color: tuple[int, int, int] = (0, 0, 0),
text_click_color: tuple[int, int, int] = (0, 0, 0),
font_size: int = 20,
font_family: str = "Arial",
centered: bool = False
position: Tuple of (x, y) coordinateswidth: Width of the buttonheight: Height of the buttonborder_radius: Radius for rounded cornersborder_color: Color of the button border, if None, the border will not be drawnborder_width: Width of the button borderlabel: Text displayed on the buttoncolor: Default button background colorhover_color: Background color when the button is hovered overclick_color: Background color when the button is clickedtext_color: Color of the text when not hoveredtext_hover_color: Color of the text when hovered overtext_click_color: Color of the text when clickedfont_size: Size of the text fontfont_family: Font family for the textcentered: If True, the button is centered on the provided position
Methods¶
All methods inherited from the Element class.
Update Method¶
update: Updates the button state based on mouse interaction and animations. Theeventsparameter should be a list of pygame events, though it's not used directly by the Button class. Calling this method is essential for proper button interaction and animation handling.
Setters¶
set_label(label: str) -> None
set_color(color: tuple[int, int, int]) -> None
set_hover_color(color: tuple[int, int, int]) -> None
set_click_color(color: tuple[int, int, int]) -> None
set_text_color(color: tuple[int, int, int]) -> None
set_text_hover_color(color: tuple[int, int, int]) -> None
set_text_click_color(color: tuple[int, int, int]) -> None
set_label: Set the text displayed on the buttonset_color: Set the default button background colorset_hover_color: Set the background color when the button is hovered overset_click_color: Set the background color when the button is clickedset_text_color: Set the color of the button label when not hoveredset_text_hover_color: Set the color of the button label when hovered overset_text_click_color: Set the color of the button label when clicked
Mouse Interaction Methods¶
is_hovered: Check if the mouse is currently hovering over the buttonis_clicked: Check if the button is currently being pressed (mouse button held down)was_clicked: Check if the button was clicked (mouse pressed and released on the button) For all mouse methods, thebuttonparameter specifies which mouse button to check (0=left, 1=middle, 2=right)