Python Scripts for Changing Windows Wallpaper, Locking the Screen, and Generating Infinite Pop‑ups
This tutorial demonstrates how to use Python with the win32api, win32gui, and ctypes libraries on Windows 10 to programmatically modify the desktop wallpaper, lock the workstation repeatedly, and spawn endless command‑prompt pop‑ups, including full source code and packaging instructions.
This article provides a step‑by‑step guide for creating three mischievous Windows utilities using Python 3.7: a wallpaper changer, an infinite lock‑screen script, and a pop‑up spawner.
Environment : Windows 10, Python 3.7, required packages pywin32 (win32api, win32con, win32gui) and ctypes .
Install the win32 package:
pip install pywin321. Change Desktop Wallpaper
The wallpaper is stored in the registry under HKEY_CURRENT_USER\Control Panel\Desktop . The script opens this key, sets WallpaperStyle to the desired mode (e.g., 2 for stretch) and calls SystemParametersInfo to apply the image.
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, 'Control PanelDesktop', 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, '2')
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)The full script iterates over a folder of images, selects each one, updates the registry, and pauses 10 seconds between changes.
import win32api, win32con, win32gui, os, time
def set_wallpaper():
path = os.listdir(r'图片文件夹')
for i in path:
img_path = r'图片文件夹' + "\\" + i
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, 'Control PanelDesktop', 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, '2')
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)
time.sleep(10)
set_wallpaper()2. Infinite Lock‑Screen
Using ctypes to load user32.dll and call LockWorkStation inside an endless loop creates a continuously locking screen.
from ctypes import windll
def lock_windows():
while True:
user = windll.LoadLibrary("user32.dll")
user.LockWorkStation()
lock_windows()Package the script with pyinstaller -F your_file.py for distribution.
3. Infinite Pop‑up Windows
By repeatedly executing os.system('start cmd') a large number of command‑prompt windows are opened. A complementary command can close them all.
for i in range(2000):
os.system('start cmd')
# To close all cmd windows
start taskkill /f /im cmd.exe /tAll source code is provided, and readers are encouraged to test the scripts at their own risk.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.