<p>对于64位windows,请使用:</p>
<pre><code>ctypes.windll.user32.SystemParametersInfoW
</code></pre>
<p>对于32位窗口,请使用:</p>
^{pr2}$
<p>如果你用错了,就会出现黑屏。您可以在<strong>控制面板->系统和安全->系统中找到您使用的版本。在</p>
<p>您还可以让脚本选择正确的脚本:</p>
<pre><code>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)
</code></pre>
<p><strong>更新:</strong></p>
<p>我犯了一个以上的疏忽。正如注释中的<a href="https://stackoverflow.com/questions/53878508/change-windows-10-background-in-python-3/53889041#comment94624531_53878546">@Mark Tolonen</a>所示,它依赖于ANSI和UNICODE路径字符串,而不是OS类型。在</p>
<p>如果使用字节字符串路径,例如<code>b'C:\\Users\\Patrick\\Desktop\\0200200220.jpg'</code>,请使用:</p>
^{pr2}$
<p>否则,您可以将其用于普通unicode路径:</p>
<pre><code>ctypes.windll.user32.SystemParametersInfoW
</code></pre>
<p>使用<a href="https://stackoverflow.com/questions/53878508/change-windows-10-background-in-python-3/53889041#53889041">@Mark Tolonen's</a>应答中的argtypes,以及另一个<a href="https://stackoverflow.com/questions/44867820/python-difference-between-windows-systemparametersinfow-vs-systemparametersinf/44875514#44875514">answer</a>中的argtypes,也可以更好地突出显示这一点。在</p>