Fundamentals 5 min read

Python Windows Automation: Change Wallpaper, Lock Screen, and Create Infinite Pop‑ups

This tutorial demonstrates how to use Python 3.7 on Windows 10 with win32api, win32gui, ctypes, and os modules to programmatically change the desktop wallpaper, lock the workstation, and spawn endless command‑prompt pop‑ups, including full source code and packaging instructions.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Windows Automation: Change Wallpaper, Lock Screen, and Create Infinite Pop‑ups

This article provides a step‑by‑step guide for Windows automation using Python 3.7. It covers three projects: modifying the desktop wallpaper, locking the screen indefinitely, and generating endless pop‑up windows.

Tools and environment

Development environment: Python 3.7 on Windows 10

Libraries: win32api , win32con , win32gui , ctypes , os , random , time

Installation command: <code>pip install pywin32</code>

1. Change Desktop Wallpaper

The wallpaper settings are stored in the registry under HKEY_CURRENT_USER\Control Panel\Desktop . The script opens this key, sets the WallpaperStyle value, and calls win32gui.SystemParametersInfo to apply a new image.

<code>k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, 'Control PanelDesktop', 0, win32con.KEY_SET_VALUE)</code>
<code>win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)</code>

Full source:

<code>import win32api   # Windows low‑level API
import win32con   # Constants for registry and system calls
import win32gui   # GUI functions
import os
import random
import time

def set_wallpaper():
    path = os.listdir(r'图片文件夹')
    for i in path:
        img_path = r'图片文件夹' + "\\" + i
        print(img_path)
        # Open registry key
        k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, 'Control PanelDesktop', 0, win32con.KEY_SET_VALUE)
        # 2 = stretch, 0 = center, 6 = fit, 10 = fill
        win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, '2')
        win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)
        time.sleep(10)

set_wallpaper()</code>

2. Infinite Lock Screen

Using the ctypes library to call the Windows user32.dll function LockWorkStation inside an endless loop.

<code>def lock_windows():
    while True:
        user = windll.LoadLibrary("user32.dll")
        user.LockWorkStation()
lock_windows()</code>

3. Infinite Pop‑up Windows

The script repeatedly launches a new Command Prompt window via os.system('start cmd') . A warning is included for low‑performance machines and a command to close all opened cmd windows.

<code>for i in range(2000):
    os.system('start cmd')
# To close all cmd windows:
# start taskkill /f /im cmd.exe /t</code>

Packaging the scripts into a standalone executable can be done with pyinstaller -F your_script.py , allowing distribution to friends or colleagues.

Images in the original article illustrate registry paths and the visual effect of the wallpaper change.

Pythonautomationwindowspyinstallerctypeswin32api
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.