Python 3.4更改桌面背景图像不起作用

时间:2016-06-26 22:54:21

标签: python windows-7 background-image python-3.4

我在SO和Google上尝试了所有示例,但没有一个能够正常运行。我不知道为什么,脚本完成没有任何错误。但背景图像不会改变。我为该图像设置了绝对路径,我尝试了jpg,png格式,基本上我尝试了所有内容,但所有示例都没有任何错误,但背景图像没有改变。这有一个有效的例子吗? Windows-7 Python 3.4

有些例子不起作用;

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

#This example can't find images, but I put absolute path to it. Don't know what's the problem
import struct
import ctypes


SPI_SETDESKWALLPAPER = 20
WALLPAPER_PATH = 'C:\\your_file_name.jpg'


def is_64_windows():
    """Find out how many bits is OS. """
    return struct.calcsize('P') * 8 == 64


def get_sys_parameters_info():
    """Based on if this is 32bit or 64bit returns correct version of SystemParametersInfo function. """
    return ctypes.windll.user32.SystemParametersInfoW if is_64_windows() \
        else ctypes.windll.user32.SystemParametersInfoA


def change_wallpaper():
    sys_parameters_info = get_sys_parameters_info()
    r = sys_parameters_info(SPI_SETDESKWALLPAPER, 0, WALLPAPER_PATH, 3)

    # When the SPI_SETDESKWALLPAPER flag is used,
    # SystemParametersInfo returns TRUE
    # unless there is an error (like when the specified file doesn't exist).
    if not r:
        print(ctypes.WinError())


change_wallpaper()

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

import struct
import ctypes
import os

def is_64_windows():
    """Find out how many bits is OS. """
    return 'PROGRAMFILES(X86)' in os.environ

def get_sys_parameters_info():
    """Based on if this is 32bit or 64bit returns correct version of SystemParametersInfo function. """
    return ctypes.windll.user32.SystemParametersInfoW if is_64_windows() \
        else ctypes.windll.user32.SystemParametersInfoA

def change_wallpaper():
    sys_parameters_info = get_sys_parameters_info()
    r = sys_parameters_info(SPI_SETDESKWALLPAPER, 0, WALLPAPER_PATH, 3)
    if not r:           # When the SPI_SETDESKWALLPAPER flag is used, SystemParametersInfo returns TRUE unless there is an error (like when the specified file doesn't exist).
        print(ctypes.WinError())

SPI_SETDESKWALLPAPER = 20
WALLPAPER_PATH = 'C:\\your_file_name.jpg'
change_wallpaper()

我认为你的问题是你有64个窗口但是32个python,然后你的is_64_windows()函数返回False但它实际上是True'PROGRAMFILES(X86)' in os.environ应该有效。