Change your Windows background by running a python script!

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 loo…


This content originally appeared on DEV Community and was authored by Matin Najafi

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()

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'

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()

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)


This content originally appeared on DEV Community and was authored by Matin Najafi


Print Share Comment Cite Upload Translate Updates
APA

Matin Najafi | Sciencx (2021-05-13T08:21:09+00:00) Change your Windows background by running a python script!. Retrieved from https://www.scien.cx/2021/05/13/change-your-windows-background-by-running-a-python-script/

MLA
" » Change your Windows background by running a python script!." Matin Najafi | Sciencx - Thursday May 13, 2021, https://www.scien.cx/2021/05/13/change-your-windows-background-by-running-a-python-script/
HARVARD
Matin Najafi | Sciencx Thursday May 13, 2021 » Change your Windows background by running a python script!., viewed ,<https://www.scien.cx/2021/05/13/change-your-windows-background-by-running-a-python-script/>
VANCOUVER
Matin Najafi | Sciencx - » Change your Windows background by running a python script!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/13/change-your-windows-background-by-running-a-python-script/
CHICAGO
" » Change your Windows background by running a python script!." Matin Najafi | Sciencx - Accessed . https://www.scien.cx/2021/05/13/change-your-windows-background-by-running-a-python-script/
IEEE
" » Change your Windows background by running a python script!." Matin Najafi | Sciencx [Online]. Available: https://www.scien.cx/2021/05/13/change-your-windows-background-by-running-a-python-script/. [Accessed: ]
rf:citation
» Change your Windows background by running a python script! | Matin Najafi | Sciencx | https://www.scien.cx/2021/05/13/change-your-windows-background-by-running-a-python-script/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.