在 Python 3 中更改 Windows 10 背景

2023-06-05Python开发问题
6

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

问题描述

我一直在尝试寻找通过 python 脚本更改 Windows 10 桌面壁纸的最佳方法.当我尝试运行此脚本时,桌面背景变为纯黑色.

I've been trying to find the best way to change the Windows 10 Desktop wallpaper through a python script. When I try to run this script, the desktop background turns to a solid black color.

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

What can I do to fix this? I'm using python3

推荐答案

对于 64 位 windows,使用:

For 64 bit windows, use:

ctypes.windll.user32.SystemParametersInfoW

对于 32 位窗口,使用:

for 32 bit windows, use:

ctypes.windll.user32.SystemParametersInfoA

如果你用错了,你会得到一个黑屏.您可以在控制面板 -> 系统和安全 -> 系统中找到您使用的版本.

If you use the wrong one, you will get a black screen. You can find out which version your using in Control Panel -> System and Security -> System.

你也可以让你的脚本选择正确的:

You could also make your script choose the correct one:

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 路径字符串,而不是操作系统类型.

I've made an oversight with the above. As @Mark Tolonen demonstrated in the comments, it depends on ANSI and UNICODE path strings, not the OS type.

如果您使用字节字符串路径,例如 b'C:\Users\Patrick\Desktop\0200200220.jpg',请使用:

If you use the byte strings paths, such as b'C:\Users\Patrick\Desktop\0200200220.jpg', use:

ctypes.windll.user32.SystemParametersInfoA

否则,您可以将其用于普通的 unicode 路径:

Otherwise you can use this for normal unicode paths:

ctypes.windll.user32.SystemParametersInfoW

@Mark Tolonen 中的 argtypes 也可以更好地突出显示这一点 答案,以及其他 answer.

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

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11