Resizable Pygame Window with Scaling UI
Here's a Python program that creates a resizable window with a GUI that scales with the window size. It uses both pygame and pygame-menu libraries to create a multi-page interface with a collapsible sidebar containing a scale.
import pygame
import pygame_menu
from pygame.locals import *
# Initialize pygame
pygame.init()
# Set initial window size
WIDTH, HEIGHT = 800, 600
window_size = (WIDTH, HEIGHT)
# Create a resizable window
screen = pygame.display.set_mode(window_size, RESIZABLE)
pygame.display.set_caption("Resizable Window Example")
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
DARK_GRAY = (100, 100, 100)
# Sidebar properties
sidebar_expanded = True
sidebar_width = 200
def create_main_menu():
"""Create the main menu with a square button"""
menu = pygame_menu.Menu(
height=window_size[1],
width=window_size,
title='Main Menu',
theme=pygame_menu.themes.THEME_BLUE.copy()
)
# Calculate button size based on window dimensions to keep it square
# and have it scale with window size
button_size = min(window_size, window_size[1]) // 4
# Add square button to go to second page
button = menu.add.button('Go to Second Page', on_button_click)
# Make button square by resizing it
button.resize(button_size, button_size)
return menu
def create_second_menu():
"""Create the second menu"""
menu_width = window_size - (sidebar_width if sidebar_expanded else 0)
menu = pygame_menu.Menu(
height=window_size[1],
width=menu_width,
title='Second Page',
theme=pygame_menu.themes.THEME_DARK.copy()
)
# Add button to return to main menu
menu.add.button('Back to Main Menu', on_back_click)
# Add button to toggle sidebar
menu.add.button('Toggle Sidebar', toggle_sidebar)
return menu
def on_button_click():
"""Handle button click to navigate to second page"""
global current_menu
current_menu = second_menu
def on_back_click():
"""Handle back button to return to main menu"""
global current_menu
current_menu = main_menu
def toggle_sidebar():
"""Toggle the sidebar expansion state"""
global sidebar_expanded, second_menu, current_menu
sidebar_expanded = not sidebar_expanded
# Recreate second menu with new dimensions
second_menu = create_second_menu()
current_menu = second_menu
def draw_sidebar():
"""Draw the sidebar with a scale on the right side of the window"""
if sidebar_expanded:
# Draw sidebar background
sidebar_rect = pygame.Rect(
window_size - sidebar_width, 0,
sidebar_width, window_size[1]
)
pygame.draw.rect(screen, GRAY, sidebar_rect)
# Draw sidebar border
pygame.draw.line(
screen, BLACK,
(window_size - sidebar_width, 0),
(window_size - sidebar_width, window_size[1]),
3
)
# Draw scale
scale_width = sidebar_width * 0.7
scale_height = window_size[1] * 0.6
scale_x = window_size - sidebar_width + (sidebar_width - scale_width) / 2
scale_y = (window_size[1] - scale_height) / 2
# Draw scale background
scale_rect = pygame.Rect(scale_x, scale_y, scale_width, scale_height)
pygame.draw.rect(screen, WHITE, scale_rect)
# Draw scale markings
for i in range(11):
y_pos = scale_y + (scale_height * i / 10)
line_start = (scale_x, y_pos)
line_end = (scale_x + scale_width * 0.3, y_pos)
pygame.draw.line(screen, BLACK, line_start, line_end, 2)
# Add labels for every other marking
if i % 2 == 0:
font = pygame.font.SysFont(None, int(sidebar_width * 0.15))
text = font.render(str(10 - i), True, BLACK)
screen.blit(text, (scale_x + scale_width * 0.4, y_pos - 10))
# Draw scale border
pygame.draw.rect(screen, BLACK, scale_rect, 2)
# Create initial menus
main_menu = create_main_menu()
second_menu = create_second_menu()
current_menu = main_menu
# Main game loop
clock = pygame.time.Clock()
running = True
while running:
# Process events
events = pygame.event.get()
for event in events:
if event.type == QUIT:
running = False
# Handle window resize event
if event.type == VIDEORESIZE:
# Update window size
window_size = (event.w, event.h)
screen = pygame.display.set_mode(window_size, RESIZABLE)
# Recreate menus with new dimensions
main_menu = create_main_menu()
second_menu = create_second_menu()
# Update current menu reference
if current_menu == main_menu:
current_menu = main_menu
else:
current_menu = second_menu
# Fill screen with background color
screen.fill(DARK_GRAY)
# Update and draw current menu
if current_menu:
current_menu.update(events)
current_menu.draw(screen)
# Draw sidebar if we're on the second page
if current_menu == second_menu:
draw_sidebar()
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(60)
# Quit pygame
pygame.quit()
How It Works
This program creates a resizable Pygame window with two pages that you can navigate between:
Main Page
- Features a square button that scales proportionally with the window size
- The button takes you to the second page when clicked
- Uses the blue theme from pygame-menu
Second Page
- Has navigation buttons to go back to the main page
- Includes a collapsible sidebar on the right side
- The sidebar contains a vertical scale with numeric markings
- A "Toggle Sidebar" button lets you show/hide the sidebar
Resizing Functionality
- The window can be resized by dragging its edges
- The GUI elements automatically scale to fit the new window dimensions
- Both the button on the main page and the sidebar on the second page adjust accordingly
Implementation Details
- The program uses pygame's RESIZABLE flag to enable window resizing
- When a VIDEORESIZE event is detected, the menus are recreated with the new dimensions
- The square button's size is calculated as a proportion of the window's smaller dimension
- The sidebar's width remains fixed, but its height scales with the window
- The scale inside the sidebar uses proportional sizing to maintain its appearance
This example demonstrates how to create a responsive GUI that adapts to different window sizes while maintaining its proportions and usability[1][4][8].