Here's a Python program with a resizable window using Pygame and pygame-menu that scales with window size, contains a square button for navigation, and includes a collapsible sidebar with a scale on the second page:
import pygame
import pygame_menu
import sys
# Initialize pygame
pygame.init()
# Constants
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
WINDOW_SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT)
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
BLUE = (0, 100, 255)
RED = (255, 0, 0)
# Create a resizable window
screen = pygame.display.set_mode(WINDOW_SIZE, pygame.RESIZABLE)
pygame.display.set_caption("Resizable Window Demo")
# Second page state variables
sidebar_expanded = True
sidebar_width = 200
scale_value = 50 # Initial scale value
# Function to go back to main menu
def go_to_main_menu():
main_menu()
# Second page implementation
def second_page():
global screen, sidebar_expanded, sidebar_width, scale_value
clock = pygame.time.Clock()
running = True
dragging_scale = False
while running:
# Get current screen size
current_width, current_height = screen.get_size()
# Calculate UI elements based on current screen size
content_width = current_width - (sidebar_width if sidebar_expanded else 20)
# Main content area
content_rect = pygame.Rect(0, 0, content_width, current_height)
# Sidebar toggle button
toggle_rect = pygame.Rect(current_width - (sidebar_width if sidebar_expanded else 20), 10, 10, 30)
# Back button - scales with window size
back_button_size = min(current_width, current_height) // 12
back_button_rect = pygame.Rect(20, 20, back_button_size, back_button_size // 2)
# Scale elements in sidebar
if sidebar_expanded:
scale_rect = pygame.Rect(current_width - sidebar_width + 20, 100, sidebar_width - 40, 20)
scale_handle_pos = scale_rect.x + (scale_rect.width * (scale_value / 100))
scale_handle_rect = pygame.Rect(scale_handle_pos - 5, scale_rect.y - 5, 10, 30)
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.VIDEORESIZE:
# Resize the screen
screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
if event.type == pygame.MOUSEBUTTONDOWN:
if toggle_rect.collidepoint(event.pos):
sidebar_expanded = not sidebar_expanded
if sidebar_expanded and scale_rect.collidepoint(event.pos) or (sidebar_expanded and scale_handle_rect.collidepoint(event.pos)):
dragging_scale = True
if back_button_rect.collidepoint(event.pos):
go_to_main_menu()
return
if event.type == pygame.MOUSEBUTTONUP:
dragging_scale = False
if event.type == pygame.MOUSEMOTION and dragging_scale and sidebar_expanded:
# Update scale value based on mouse position
mouse_x = event.pos
if mouse_x >= scale_rect.x and mouse_x <= scale_rect.x + scale_rect.width:
relative_x = mouse_x - scale_rect.x
scale_value = int((relative_x / scale_rect.width) * 100)
scale_value = max(0, min(100, scale_value))
# Draw content
screen.fill(WHITE)
# Draw main content area
pygame.draw.rect(screen, GRAY, content_rect)
# Add some text showing this is the second page
font = pygame.font.SysFont(None, max(24, min(current_width, current_height) // 20))
second_page_text = font.render("Second Page", True, BLACK)
text_x = content_width // 2 - second_page_text.get_width() // 2
text_y = current_height // 2 - second_page_text.get_height() // 2
screen.blit(second_page_text, (text_x, text_y))
# Draw back button
pygame.draw.rect(screen, BLUE, back_button_rect)
back_text = font.render("Back", True, WHITE)
back_text_x = back_button_rect.x + (back_button_rect.width - back_text.get_width()) // 2
back_text_y = back_button_rect.y + (back_button_rect.height - back_text.get_height()) // 2
screen.blit(back_text, (back_text_x, back_text_y))
# Draw sidebar if expanded
if sidebar_expanded:
pygame.draw.rect(screen, BLACK, pygame.Rect(current_width - sidebar_width, 0, sidebar_width, current_height))
# Draw scale
pygame.draw.rect(screen, GRAY, scale_rect)
pygame.draw.rect(screen, BLUE, scale_handle_rect)
# Draw scale value
scale_text = font.render(f"Scale: {scale_value}%", True, WHITE)
screen.blit(scale_text, (current_width - sidebar_width + 20, 70))
# Draw toggle button
pygame.draw.rect(screen, RED, toggle_rect)
toggle_text = font.render("◀" if sidebar_expanded else "▶", True, WHITE)
screen.blit(toggle_text, (toggle_rect.x - 5, toggle_rect.y + 5))
pygame.display.flip()
clock.tick(60)
# Function to go to second page
def go_to_second_page():
second_page()
# Main menu implementation using pygame-menu
def main_menu():
global screen
# Create a theme that supports dynamic resizing
mytheme = pygame_menu.Theme(
background_color=pygame.Color(30, 30, 30),
title_background_color=pygame.Color(50, 50, 50),
title_font=pygame_menu.font.FONT_DIGITAL,
title_font_size=40,
widget_font=pygame_menu.font.FONT_DIGITAL,
widget_font_color=WHITE,
widget_margin=(20, 20),
widget_background_color=BLUE
)
# Create menu with relative size
menu = pygame_menu.Menu(
height=WINDOW_HEIGHT,
theme=mytheme,
title='Main Menu',
width=WINDOW_WIDTH,
enable_resizing=True, # Enable menu resizing
resize_menu_from_display=True # Auto-resize when display changes
)
# Add square button that will scale with the window
def update_button_size():
current_width, current_height = screen.get_size()
button_size = min(current_width, current_height) // 3
button.resize(button_size, button_size)
button.update_font({'size': button_size // 10})
button = menu.add.button('Go to Second Page', go_to_second_page)
update_button_size() # Set initial size
running = True
clock = pygame.time.Clock()
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.VIDEORESIZE:
# Resize the screen
screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
menu.resize(event.w, event.h)
update_button_size() # Update button size on window resize
# Draw menu
menu.update(events)
menu.draw(screen)
pygame.display.flip()
clock.tick(60)
# Start with the main menu
if __name__ == "__main__":
main_menu()
This program features:
- A fully resizable main window that uses Pygame's RESIZABLE flag[1][3][5]
- A main menu created with pygame-menu that scales with the window size[6][9]
- A square button that takes you to a second page and scales proportionally with the window size[3]
- A second page with:
- A collapsible sidebar on the right side that can be toggled open/closed
- A scale component in the sidebar that allows dragging to change values
- A back button to return to the main menu
- UI elements that resize and reposition based on window size[2][3]
The program handles resizing events properly by recreating the display surface when the window is resized[1] and updating the positions and sizes of all UI elements to maintain proper scaling across different window dimensions.
To run this program, you'll need to have both pygame and pygame-menu installed. You can install them with pip if you don't have them already:
pip install pygame pygame-menu