使用Python更改桌面背景 [英] Changing desktop background with Python

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

问题描述

我一直在尝试使用Python 2.7更改桌面背景:

I've been tring to change the desktop background using Python 2.7 :

SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pngName, 0)

(而pngName是图像的有效路径)

( While pngName is a valid path to an image )

当我运行脚本时,没有任何变化,而当我尝试使用其他图像时,它就可以正常工作.

When I run the script nothing changes, and when I try it with a different image it works just fine.

有什么建议吗?

p.s.有人可以解释一下该脚本的工作原理吗?我从某个地方拿来的,不太了解.

p.s. can someone please explain how that script works? I've taken it from somewhere and don't quite understand it.

谢谢!

推荐答案

不要使用 png 文件(我相信您正在使用它们,因为图像路径的变量名是pngName),请尝试 jpg .似乎MS Windows不支持png作为背景图片格式(作为Linux/Mac用户,我自己无法确认,但请参见

Instead of using png files (I believe you're using them, given the variable name for image paths is pngName), try jpg. It seems that MS Windows doesn't support png as a background image format (As a Linux/Mac user, I cannot confirm it myself, but see this discussion for more details).

关于脚本的工作方式,我可以简单地说一下它使用了 ctypes Python程序包,使您可以调用dll/共享库中的函数,例如SystemParametersInfo.SystemParametersInfo(请参阅 MSDN页面)检索/设置某些系统范围参数的值,包括设置桌面参数(例如墙纸文件).请参阅示例三此处以了解更多信息.

Regarding how your script works, I can briefly say that it uses ctypes Python package that allows you to call functions in dlls/shared libraries such as SystemParametersInfo. SystemParametersInfo (see MSDN page) retrieves/sets values of some system-wide parameters, including setting Desktop parameters like wallpaper file. See Example Three here for more insights.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆