【问题标题】:Python 3.4 changing desktop background image does not workPython 3.4 更改桌面背景图像不起作用
【发布时间】:2016-10-28 21:01:11
【问题描述】:

我在 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()

【问题讨论】:

  • 你能展示你的代码吗?
  • 我尝试了10个例子,我应该把它们都放出来吗?真的吗?
  • 好吧,你没有提供太多信息来获得帮助。你能选择一个你认为应该有用的吗?
  • @joelgoldstick 我放了一些请检查
  • 这可能会有所帮助:stackoverflow.com/questions/2208828/…

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


【解决方案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 应该可以工作。

【讨论】:

  • 哇。这实际上有效。许多哇。希望我能多次投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
相关资源
最近更新 更多