Python Program with Resizable Window and Scaling UI
Here's a Python program that creates a resizable window with a GUI that scales proportionally. It includes a square button to navigate to a second page, which features a collapsible sidebar with a scale.
import pygame
import pygame_menu
from pygame_menu.locals import POSITION_RIGHT
# Initialize pygame
pygame.init()
# Initial screen dimensions
WIDTH, HEIGHT = 800, 600
window_size = (WIDTH, HEIGHT)
# Create a resizable window
screen = pygame.display.set_mode(window_size, pygame.RESIZABLE)
pygame.display.set_caption("Resizable Window Demo")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (100, 150, 255)
GRAY = (200, 200, 200)
DARK_GRAY = (100, 100, 100)
# Game states
MAIN_PAGE = 0
SECOND_PAGE = 1
current_state = MAIN_PAGE
# Sidebar state
sidebar_visible = True
sidebar_width = 200
sidebar_target_width = sidebar_width
sidebar_animation_speed = 10
# Scale value
scale_value = 50
scale_min = 0
scale_max = 100
def scale_position(pos, base_size, current_size):
"""Scale a position based on current window size"""
x_scale = current_size / base_size
y_scale = current_size[1] / base_size[1]
return (int(pos * x_scale), int(pos[1] * y_scale))
def scale_rect(rect, base_size, current_size):
"""Scale a rectangle based on current window size"""
pos = scale_position((rect, rect[1]), base_size, current_size)
size = scale_position((rect[2], rect[3]), base_size, current_size)
return (pos, pos[1], size, size[1])
def create_main_menu(surface, size):
"""Create the main menu with pygame_menu"""
theme = pygame_menu.themes.THEME_BLUE.copy()
theme.widget_font_size = int(30 * size[1] / HEIGHT)
# Calculate button size based on window size
button_size = min(size, size[1]) // 4
menu = pygame_menu.Menu(
height=size[1],
width=size,
title='Main Menu',
theme=theme
)
# Create a frame for the button
frame = menu.add.frame_v(width=button_size, height=button_size, align=pygame_menu.locals.ALIGN_CENTER)
# Add a button inside the frame
frame.pack(menu.add.button('Go to\nSecond Page', go_to_second_page,
font_size=int(20 * size[1] / HEIGHT)))
return menu
def create_second_menu(surface, size):
"""Create the second menu with pygame_menu"""
global sidebar_width, sidebar_target_width
theme = pygame_menu.themes.THEME_DARK.copy()
theme.widget_font_size = int(25 * size[1] / HEIGHT)
effective_width = size - sidebar_width if sidebar_visible else size
menu = pygame_menu.Menu(
height=size[1],
width=effective_width,
title='Second Page',
theme=theme
)
menu.add.button('Back to Main', go_to_main_page,
font_size=int(20 * size[1] / HEIGHT))
menu.add.button('Toggle Sidebar', toggle_sidebar,
font_size=int(20 * size[1] / HEIGHT))
return menu
def go_to_second_page():
"""Switch to the second page"""
global current_state
current_state = SECOND_PAGE
def go_to_main_page():
"""Switch to the main page"""
global current_state
current_state = MAIN_PAGE
def toggle_sidebar():
"""Toggle the sidebar visibility"""
global sidebar_visible, sidebar_target_width
sidebar_visible = not sidebar_visible
if sidebar_visible:
sidebar_target_width = 200
else:
sidebar_target_width = 0
def draw_sidebar(surface, size):
"""Draw the collapsible sidebar with a scale"""
global sidebar_width, scale_value
# Animate sidebar width
if sidebar_width < sidebar_target_width:
sidebar_width = min(sidebar_width + sidebar_animation_speed, sidebar_target_width)
elif sidebar_width > sidebar_target_width:
sidebar_width = max(sidebar_width - sidebar_animation_speed, sidebar_target_width)
if sidebar_width <= 0:
return
# Draw sidebar background
sidebar_rect = (size - sidebar_width, 0, sidebar_width, size[1])
pygame.draw.rect(surface, GRAY, sidebar_rect)
pygame.draw.line(surface, BLACK, (size - sidebar_width, 0),
(size - sidebar_width, size[1]), 2)
# Draw sidebar title
font = pygame.font.SysFont('Arial', int(20 * size[1] / HEIGHT))
title = font.render('Sidebar', True, BLACK)
title_pos = (size - sidebar_width + 10, 20)
surface.blit(title, title_pos)
# Draw scale label
scale_label = font.render(f'Scale: {scale_value}', True, BLACK)
scale_label_pos = (size - sidebar_width + 10, 80)
surface.blit(scale_label, scale_label_pos)
# Draw scale track
scale_track_start = (size - sidebar_width + 20, 120)
scale_track_end = (size - 20, 120)
scale_track_width = scale_track_end - scale_track_start
pygame.draw.line(surface, BLACK, scale_track_start, scale_track_end, 3)
# Draw scale handle
handle_x = scale_track_start + (scale_track_width * scale_value / (scale_max - scale_min))
handle_rect = (handle_x - 5, scale_track_start[1] - 10, 10, 20)
pygame.draw.rect(surface, BLUE, handle_rect)
# Check for scale interaction
mouse_pos = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()
if mouse_pressed and sidebar_rect <= mouse_pos <= sidebar_rect + sidebar_rect[2]:
if scale_track_start[1] - 15 <= mouse_pos[1] <= scale_track_start[1] + 15:
# Calculate new scale value based on mouse position
relative_x = mouse_pos - scale_track_start
scale_value = int(max(min(relative_x / scale_track_width * (scale_max - scale_min), scale_max), scale_min))
def main():
"""Main program loop"""
global current_state, window_size
clock = pygame.time.Clock()
running = True
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
if event.type == pygame.VIDEORESIZE:
window_size = event.size
screen = pygame.display.set_mode(window_size, pygame.RESIZABLE)
screen.fill(WHITE)
# Handle different pages
if current_state == MAIN_PAGE:
main_menu = create_main_menu(screen, window_size)
main_menu.update(events)
main_menu.draw(screen)
elif current_state == SECOND_PAGE:
second_menu = create_second_menu(screen, window_size)
second_menu.update(events)
second_menu.draw(screen)
draw_sidebar(screen, window_size)
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
How This Program Works
Main Structure
The program is structured around a state-based architecture that manages two distinct pages:
- A main page with a square button for navigation
- A second page with a functional collapsible sidebar containing a scale
Resizable Window Features
The window is made resizable using the pygame.RESIZABLE flag when setting up the display mode. All UI elements scale proportionally to the window size through scaling functions that calculate positions and dimensions based on the current window dimensions[1][3].
GUI Scaling
The program handles window resizing events (pygame.VIDEORESIZE) by recalculating all UI components based on the new dimensions. The square button and other elements are sized relative to the window dimensions, ensuring they scale properly when the window is resized[2][4].
Navigation System
The square button on the main page triggers the go_to_second_page() function, which changes the application state to display the second page. Similarly, the "Back to Main" button on the second page returns to the main page[6][9].
Collapsible Sidebar
The sidebar on the second page can be toggled using a dedicated button. When toggled, the sidebar smoothly animates in or out of view using a simple animation system that gradually adjusts the sidebar width until it reaches the target width[7].
Interactive Scale
The sidebar contains a functional scale that can be adjusted by clicking and dragging. The scale's value (from 0 to 100) is visually represented with a blue handle that moves along a track. The current value is displayed above the scale[10].
This program demonstrates effective use of both the pygame and pygame-menu libraries to create a responsive, interactive UI that works well with window resizing, exactly as requested.