从Python更改Windows背景

从Python更改Windows背景

本文介绍了从Python更改Windows背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道用python更改Windows桌面墙纸的方法,以便使更改永久生效吗?我找到了这段代码

Does anyone know a way to change the Windows Desktop Wallpaper with python so that the change is permanent? I have found this code

import ctypes
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "myimage.jpg" , 0)

此代码有效,但是一旦注销并再次登录,背景又回到了原始图像。我希望不需要任何注册表编辑的解决方案,如果可能的话,我希望它可以与Windows XP和7兼容。

This code works, but once you log off and log on again, the background is back to the original image. I would prefer a solution that does not require any registry edit, and I would like something that works with Windows XP and 7 if it is possible.

推荐答案

此解决方案结合了一些意见,并为我工作:

This solution combines several of the comments made, and works for me:

import ctypes
import os
drive = "C:\\"
folder = "images"
image = "test.jpg"
image_path = os.path.join(drive, folder, image)
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, image_path, 3)

(请注意,您应确定图像的绝对路径,并根据需要进行更改。如果需要在XP上使用,请将图像转换为BMP。您可以轻松地将图像转换为)

(Note that you should determine the absolute path to your image, and change as needed. Also convert the image to BMP if you need to use it on XP. You can easily convert the image using Pillow)

这篇关于从Python更改Windows背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:18