Python Scripts for Changing Windows Wallpaper, Locking the Screen, and Creating Infinite Pop‑up Windows
This tutorial demonstrates how to use Python with win32api, ctypes, and os modules on Windows 10 to programmatically modify the desktop wallpaper via the registry, lock the workstation in an infinite loop, and spawn endless command‑prompt windows, including full code examples and packaging instructions.
This article provides step‑by‑step instructions for three Windows automation tasks using Python 3.7 on Windows 10.
1. Change Desktop Wallpaper – Required packages are pywin32 (install with pip install pywin32 ) and standard libraries os , random , time . The wallpaper settings are stored in the registry under HKEY_CURRENT_USER\Control Panel\Desktop . The script opens the key, sets WallpaperStyle to 2 (stretch), and calls win32gui.SystemParametersInfo to apply the image.
<code>pip install pywin32</code> <code>import win32api
import win32con
import win32gui
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 Panel\Desktop', 0, win32con.KEY_SET_VALUE)
# Set stretch mode (2)
win32api.RegSetValueEx(k, 'WallpaperStyle', 0, win32con.REG_SZ, '2')
# Apply wallpaper
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)
time.sleep(10)
set_wallpaper()</code>2. Infinite Lock Screen – Uses the ctypes library to call the Windows API function LockWorkStation from user32.dll . The function is placed inside an endless while True loop.
<code>from ctypes import windll
def lock_windows():
while True:
user = windll.LoadLibrary('user32.dll')
user.LockWorkStation()
lock_windows()</code>3. Infinite Pop‑up Windows – Employs the os module to repeatedly launch a new Command Prompt window, creating a flood of pop‑ups. A command to terminate all opened cmd processes is also provided.
<code>for i in range(2000):
os.system('start cmd')
# To close all cmd windows:
# start taskkill /f /im cmd.exe /t</code>Finally, the article mentions packaging the scripts into standalone executables with pyinstaller -F your_script.py and advises adding delays to avoid overwhelming low‑spec machines.
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.