import pygame import pygame_gui # Initialize Pygame pygame.init() # Set up the window window_size = (800, 600) window_surface = pygame.display.set_mode(window_size) pygame.display.set_caption("Scrollable Text Box Example") # Initialize UIManager manager = pygame_gui.UIManager(window_size) # Define the text box rectangle (position and size) text_box_rect = pygame.Rect(100, 100, 400, 300) # Create the text box with initial empty content text_box = pygame_gui.elements.UITextBox( html_text="", relative_rect=text_box_rect, manager=manager ) # Store text content as a list of lines text_content = [] def append_text(new_text): text_content.append(new_text) # Join lines with
for HTML line breaks html_text = "
".join(text_content) text_box.html_text = html_text text_box.rebuild() # Rebuild to update the text # Auto-scroll to the bottom if text_box.vertical_scroll_bar: # Set scroll position to the maximum text_box.vertical_scroll_bar.scroll_position = ( text_box.vertical_scroll_bar.scrollable_height ) clock = pygame.time.Clock() is_running = True while is_running: time_delta = clock.tick(60) / 1000.0 # Limit to 60 FPS # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: is_running = False # Pass events to pygame_gui manager.process_events(event) # Example: Append text when Enter is pressed if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: append_text("New message at the bottom!") # Update UI manager.update(time_delta) # Draw the UI window_surface.fill((255, 255, 255)) # White background manager.draw_ui(window_surface) pygame.display.update() pygame.quit()