0

I tried every example on SO and Google, but none of them working. I don't know why, script finishes without any error. But background image does not changing. I put absolute path for that image, I tried jpg,png formats, basically I tried everything but all examples finished without any error and yet background image doesn't changed. Is there a working example for this? Windows-7 Python 3.4

Some examples didn't work;

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()
6
  • can you show your code? Commented Jun 26, 2016 at 22:55
  • There are 10 examples I tried, should I put them all? Really? Commented Jun 26, 2016 at 22:55
  • well, you aren't giving much info to get help. Can you pick one you think should work? Commented Jun 26, 2016 at 22:58
  • @joelgoldstick I put some of them check please Commented Jun 26, 2016 at 23:02
  • 1
    This might help: stackoverflow.com/questions/2208828/… Commented Jun 27, 2016 at 0:07

1 Answer 1

1

try using following code:

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

I think your problem is that you have 64 windows but 32 python, a then your is_64_windows() function returns False but it is actually True, 'PROGRAMFILES(X86)' in os.environ should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow. This actually works. Many wow. Wish I can upvote more than once.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.