How to Create a Windows Screensaver from a Python Script

spyboy's avatarPosted by

Creating a .scr file, which is a screensaver file for Windows, is straightforward. Screensavers are essentially executable files (.exe) renamed with the .scr extension. Here’s how you can create a simple .scr file:


Steps to Create a Screensaver

1. Write the Code

Use a programming language like Python or C# to create the screensaver functionality. Below is an example using Python with the pygame library to display a moving object as a screensaver.


Python Example: Moving Ball Screensaver

import pygame
import random
import sys

# Initialize pygame
pygame.init()

# Screen dimensions
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height), pygame.NOFRAME)

# Colors
black = (0, 0, 0)
white = (255, 255, 255)

# Ball properties
ball_radius = 20
ball_x = random.randint(ball_radius, screen_width - ball_radius)
ball_y = random.randint(ball_radius, screen_height - ball_radius)
ball_speed_x = 3
ball_speed_y = 3

# Clock
clock = pygame.time.Clock()

# Screensaver loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.KEYDOWN:
            pygame.quit()
            sys.exit()

    # Move the ball
    ball_x += ball_speed_x
    ball_y += ball_speed_y

    # Bounce the ball off the walls
    if ball_x - ball_radius < 0 or ball_x + ball_radius > screen_width:
        ball_speed_x *= -1
    if ball_y - ball_radius < 0 or ball_y + ball_radius > screen_height:
        ball_speed_y *= -1

    # Draw everything
    screen.fill(black)
    pygame.draw.circle(screen, white, (ball_x, ball_y), ball_radius)
    pygame.display.flip()

    # Limit the frame rate
    clock.tick(60)


2. Convert to .exe

Use a tool like PyInstaller to convert your Python script into an executable:

pip install pyinstaller 
pyinstaller --onefile --noconsole your_script.py

The executable will be located in the dist folder.


3. Rename to .scr

Rename the generated .exe file to have a .scr extension:

mv your_script.exe your_screensaver.scr


4. Place in the Windows System Folder

Copy the .scr file to the Windows system directory:

C:\Windows\System32


5. Test Your Screensaver

  • Right-click on the desktop and select Personalize.
  • Go to Lock screen > Screen saver settings.
  • Select your screensaver from the dropdown list.

Optional Features

  • Add Settings Support: Detect command-line arguments like /c (settings), /p (preview), or /s (start) for more functionality.
  • Multiple Displays: Adapt the code to handle multiple monitors using libraries like pygetwindow.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.