DEV Community

loading...
Cover image for Change your Windows background by running a python script!

Change your Windows background by running a python script!

Matin
Teen Software developer, tech youtuber and blogger :)
Updated on ・2 min read

Let's write some fun codes with Python :) Before we get started, be sure to note that this post is only for Windows users! An interesting idea for those who work with Windows.

What do you do if you want to change your desktop background using python? You are probably going to a library or looking for a library, but this does not require a library in Windows, and you can change the background without installing a special library and using ctypes.
In fact, the desktop background can be changed using a commonly used command to change system parameters in ctypes. (let's write the program object-oriented so that our work is clean and orderly.)

import ctypes

class Main:
    def __init__(self):
        path = 'c:/...'
        ctypes.windll.user32.SystemParametersInfoW(20, 0, path , 0)

application = Main()
Enter fullscreen mode Exit fullscreen mode

It is easy to change your desktop wallpaper temporarily by giving the address of the png, jpg, etc. file.
Now let's make the program a little more attractive, suppose we have a folder called "backgrounds" and we want to randomly select an image from it and set it on our desktop background.
First we need to get the address of where the project is now located. We can do this using os and sys

>>> import os
>>> import sys
>>> os.path.abspath(os.path.dirname(sys.argv[0]))
'C:\\Users\\user'
Enter fullscreen mode Exit fullscreen mode

Now just take the "backgrounds" folder and download the files in it and put them in a list. Of course, note that we receive files that end with png, jpg and jpeg, Then select a random item and order it to change the desktop background randomly. (To select a random option from a list we need a random library)


import os
import sys
import ctypes
import random

class Main:
    def __init__(self):
        self.path = os.path.abspath(os.path.dirname(sys.argv[0]))
        for root, directories, files in os.walk(os.path.join(self.path, 'backgrounds')):
            self.backgrounds = [file.lower() for file in files if file.endswith(('.png', '.jpg', '.jpeg'))]

        ctypes.windll.user32.SystemParametersInfoW(20, 0, os.path.join(self.path, 'backgrounds', random.choice(self.backgrounds)) , 0)

application = Main()
Enter fullscreen mode Exit fullscreen mode

We are done! Now we can put the images we want in the "backgrounds" folder and temporarily set a random background on our desktop every time we run this script.

An interesting point!

If you want to run this script automatically when you turn on your computer, you can open the 'run' window with the key combination (win + r) and enter the 'shell: startup'. After entering, a folder will open for you. Just copy the script to this folder. (Every program in this folder runs when you turn on your computer)

Discussion (0)