DropdownMenu¶
The DropdownMenu
provides a dropdown interface for selecting one option from a list of choices. It supports customizable colors, text styles, and the ability to wrap options into multiple columns.
Basic Usage¶
# Create a dropdown menu
dropdown = pygameui.DropdownMenu(
position=(100, 100),
options=["Option 1", "Option 2", "Option 3"],
width=200,
height=50
)
# In the main loop
events = pygame.event.get()
dropdown.update(events) # Pass events for proper interaction
dropdown.draw(screen)
# Get the selected option
selected_option = dropdown.get_selected_option()
Properties¶
position: tuple[int, int],
width: int = 200,
height: int = 50,
options: list[str],
on_change: callable = None,
element_width: int = 200,
element_height: int = 50,
element_spacing: int = 2,
max_elements_per_column: int = 5,
wrap_reverse: bool = False,
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),
font_size: int = 20,
font_family: str = "Arial",
text_color: tuple[int, int, int] = (0, 0, 0),
text_hover_color: tuple[int, int, int] = (0, 0, 0),
text_click_color: tuple[int, int, int] = (0, 0, 0),
selected_option_color: tuple[int, int, int] = (200, 200, 200),
selected_option_hover_color: tuple[int, int, int] = (150, 150, 150),
selected_option_click_color: tuple[int, int, int] = (100, 100, 100),
selected_option_text_color: tuple[int, int, int] = (0, 0, 0),
selected_option_text_hover_color: tuple[int, int, int] = (0, 0, 0),
selected_option_text_click_color: tuple[int, int, int] = (0, 0, 0),
border_radius: int = 0,
centered: bool = False
position
: Tuple of (x, y) coordinateswidth
: Width of the dropdown menu main buttonheight
: Height of the dropdown menu main buttonoptions
: List of strings representing the dropdown optionson_change
: Function called when the selected option changeselement_width
: Width of each option button in the dropdownelement_height
: Height of each option button in the dropdownelement_spacing
: Spacing between option buttonsmax_elements_per_column
: Maximum number of options per column before wrappingwrap_reverse
: If True, wraps options in reverse ordercolor
: Default background color of the option buttonshover_color
: Background color when an option is hovered overclick_color
: Background color when an option is clickedfont_size
: Size of the text fontfont_family
: Font family used for texttext_color
: Default text colortext_hover_color
: Text color when hovered overtext_click_color
: Text color when clickedselected_option_color
: Background color of the main buttonselected_option_hover_color
: Background color of the main button when hoveredselected_option_click_color
: Background color of the main button when clickedselected_option_text_color
: Text color of the main buttonselected_option_text_hover_color
: Text color of the main button when hoveredselected_option_text_click_color
: Text color of the main button when clickedborder_radius
: Radius for rounded cornerscentered
: If True, the dropdown is centered on the provided position
Methods¶
All methods inherited from the Element class.
Setters¶
set_options(options: list[str]) -> None
set_selected_option(option: str) -> None
set_selected_index(index: int) -> None
set_options
: Set the list of options for the dropdownset_selected_option
: Set the currently selected option by valueset_selected_index
: Set the currently selected option by index
Getters¶
get_selected_option
: Get the currently selected optionget_selected_index
: Get the index of the currently selected optionget_options
: Get the list of options
Mouse and Click Events¶
See the Mouse and Click Events section in the Element documentation.
Example¶
Simple example of a dropdown menu with selectable options.
import pygame
import pygameui
# Initialize
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
# Create dropdown menu
dropdown = pygameui.DropdownMenu(
position=(400, 200),
options=["Option 1", "Option 2", "Option 3"],
width=250,
height=50,
color=(240, 240, 240),
hover_color=(220, 220, 220),
text_color=(50, 50, 50),
centered=True
)
# Main loop
running = True
while running:
# Handle events
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
# Reset screen
screen.fill((30, 30, 30)) # Dark background
# Update dropdown
dropdown.update(events)
# Check for selection
if dropdown.get_selected_option():
print(f"Selected: {dropdown.get_selected_option()}")
# Draw elements
dropdown.draw(screen)
# Update display
pygame.display.flip()
clock.tick(60)