How to change desktop wallpaper in Python

Free Python Code - Aug 18 '23 - - Dev Community

Hi 🙂🖐

Welcome everyone! In this post, I will share with you how to change the desktop wallpaper using Python.

Step 1:

If you need any desktop wallpaper, you can download it from the internet.

Step 2

import Python modules


import win32con
import ctypes
import os

Enter fullscreen mode Exit fullscreen mode

Step 3

Create a function called set_desktop_wallpaper

import win32con
import ctypes
import os

def set_desktop_wallpaper(image_path):
    image_path = os.path.join(os.getcwd(), image_path)
    change = win32con.SPIF_UPDATEINIFILE or win32con.SPIF_SENDCHANGE
    ctypes.windll.user32.SystemParametersInfoW(win32con.SPI_SETDESKWALLPAPER, 0, image_path, 0, change)

set_desktop_wallpaper('alone.jpg')
Enter fullscreen mode Exit fullscreen mode

SPIF_UPDATE­INI­FILE
Writes the new system-wide parameter setting to the user profile.
SPIF_SEND­CHANGE
Broadcasts the WM_SETTING­CHANGE message after updating the user profile.

This info is from https://devblogs.microsoft.com/oldnewthing/20160721-00/?p=93925.

SPI_SETDESKWALLPAPER used to change the desktop wallpaper

Now we're done 🤗

Don't forget to like and follow 🙂

Support me on PayPal 🤗
https://www.paypal.com/paypalme/amr396

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .