Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to run a python script to change the Windows background

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "how to run python script to change Windows background", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to run python script to change Windows background" article.

What would you do if you wanted to use python to change the desktop background? You may have to go to a library or look for a library, but this does not require libraries in Windows, and you can change the background without installing special libraries and using ctype.

In fact, you can use common commands to change the desktop background to change the system parameters in ctype. Let's write an object-oriented program so that our work is clean and orderly.)

Import ctypesclass Main: def _ _ init__ (self): path = 'cju init__.' Ctypes.windll.user32.SystemParametersInfoW (20,0, path, 0) application = Main ()

By giving the addresses of PNG, jpg, and other files, you can easily change the desktop wallpaper temporarily.

Now let's make this program more attractive, suppose we have a folder called "background" from which we want to randomly select an image and set it on our desktop background.

First, we need to get the address where the project is now located. We can use os and sys to accomplish this task.

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

Now just pick up the background folder, download the files in it, and put them in the list. Notice, of course, that we receive files that end with png, jpg, and jpeg, then select a random item and order it to randomly change the desktop background. (to select a random option from the list, we need a random library)

Import osimport sysimport ctypesimport randomclass 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') ] ctypes.windll.user32.SystemParametersInfoW (20,0, os.path.join (self.path, 'backgrounds', random.choice (self.backgrounds)), 0) application = Main ()

Now we can put the image we want in the background folder and temporarily set a random background on the desktop each time we run the script.

An interesting point of view!

If you want to run this script automatically when you turn on your computer, you can use the key combination (win+r) to open the Run window and enter shell:start. After you enter, a folder will be opened for you. Just copy the script to this folder. (every program in this folder runs when you turn on the computer.)

The above is about "how to run the python script to change the Windows background" this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report