[英]Change Windows 10 background in Python 3
我一直在寻找通过python脚本更改Windows 10桌面墙纸的最佳方法。 当我尝试运行此脚本时,桌面背景变成纯黑色。
import ctypes
path = 'C:\\Users\\Patrick\\Desktop\\0200200220.jpg'
def changeBG(path):
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(20, 0, path, 3)
return;
changeBG(path)
我该怎么做才能解决此问题? 我正在使用python3
对于64位窗口,请使用:
ctypes.windll.user32.SystemParametersInfoW
对于32位窗口,请使用:
ctypes.windll.user32.SystemParametersInfoA
如果使用错误的屏幕,则会出现黑屏。 您可以在控制面板->系统和安全->系统中找到要使用的版本。
您还可以使脚本选择正确的脚本:
import struct
import ctypes
PATH = 'C:\\Users\\Patrick\\Desktop\\0200200220.jpg'
SPI_SETDESKWALLPAPER = 20
def is_64bit_windows():
"""Check if 64 bit Windows OS"""
return struct.calcsize('P') * 8 == 64
def changeBG(path):
"""Change background depending on bit size"""
if is_64bit_windows():
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, PATH, 3)
else:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, PATH, 3)
changeBG(PATH)
更新:
以上已对我进行了监督。 正如@Mark Tolonen在注释中所展示的那样,它取决于ANSI和UNICODE路径字符串,而不取决于OS类型。
如果使用字节字符串路径,例如b'C:\\\\Users\\\\Patrick\\\\Desktop\\\\0200200220.jpg'
,请使用:
ctypes.windll.user32.SystemParametersInfoA
否则,您可以将其用于普通的unicode路径:
ctypes.windll.user32.SystemParametersInfoW
@Mark Tolonen的答案以及其他答案中的 argtypes也更好地突出了这一点。
SystemParametersInfoA
采用ANSI字符串(Python 3中为bytes
类型)。
SystemParametersInfoW
采用Unicode字符串(Python 3中为str
类型)。
因此使用:
path = b'C:\\Users\\Patrick\\Desktop\\0200200220.jpg'
ctypes.windll.user32.SystemParametersInfoA(20, 0, path, 3)
要么:
path = 'C:\\Users\\Patrick\\Desktop\\0200200220.jpg'
ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 3)
您可以设置argtypes进行参数检查。 第三个参数记录为LPVOID
但您可以更具体地进行类型检查:
from ctypes import *
windll.user32.SystemParametersInfoW.argtypes = c_uint,c_uint,c_wchar_p,c_uint
windll.user32.SystemParametersInfoA.argtypes = c_uint,c_uint,c_char_p,c_uint
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.