Python Scripts for Changing Wallpaper, Locking the Screen, and Creating Infinite Pop‑ups on Windows
This tutorial shows how to use Python 3.7 on Windows 10 with win32api and ctypes to programmatically change the desktop wallpaper, continuously lock the workstation, and launch endless command‑prompt pop‑up windows, including full source code and packaging instructions.
This article demonstrates how to use Python 3.7 on Windows 10 to create three prank utilities: changing the desktop wallpaper, locking the screen continuously, and opening endless command‑prompt pop‑up windows.
1. Change Desktop Wallpaper
Tools
Development environment: Python 3.7, Windows 10
Libraries: win32api, win32con, win32gui, os, random
Installation command: <code>pip install pywin32</code>
The wallpaper settings are stored in the registry under HKEY_CURRENT_USER\Control Panel\Desktop . By opening this key with win32api.RegOpenKeyEx and setting WallpaperStyle and TileWallpaper , the script can apply a chosen image using win32gui.SystemParametersInfo .
<code>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)</code>Full source code is provided below.
<code>import win32api # call Windows API
import win32con # modify registry values
import win32gui # submit data to system
import os
import random
import time
def set_wallpaper():
path = os.listdir(r'图片文件夹')
for i in path:
img_path = r'图片文件夹' + "\\" + i
print(img_path)
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()
</code>2. Infinite Lock Screen
Tools
Development environment: Python 3.7, Windows 10
Library: ctypes (allows calling functions from user32.dll to lock the workstation).
<code>def lock_windows():
while True:
user = windll.LoadLibrary("user32.dll")
user.LockWorkStation()
lock_windows()
</code>Package the script with pyinstaller -F your_file.py for distribution.
3. Endless Pop‑up Windows
Using the os module to repeatedly launch cmd windows.
<code>for i in range(2000):
os.system('start cmd')
</code>To stop the pop‑ups, run: start taskkill /f /im cmd.exe /t
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.