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()
GLHF
  • 3,835
  • 10
  • 38
  • 83
  • can you show your code? – joel goldstick Jun 26 '16 at 22:55
  • There are 10 examples I tried, should I put them all? Really? – GLHF Jun 26 '16 at 22:55
  • well, you aren't giving much info to get help. Can you pick one you think should work? – joel goldstick Jun 26 '16 at 22:58
  • @joelgoldstick I put some of them check please – GLHF Jun 26 '16 at 23:02
  • 1
    This might help: http://stackoverflow.com/questions/2208828/detect-64bit-os-windows-in-python – Jacques Gaudin Jun 27 '16 at 00:07
  • You may need administrative privileges to change the wallpaper. Make sure you run Python with them (e.g. from an administrative console). There's also some funny stuff ([WoW64](https://en.wikipedia.org/wiki/WoW64)) that goes on when you run a 32-bit programs, like the 32-bit version of Python, on a 64-bit version of the OS to simulate the 32-bit version of the OS — the result being that you may not be changing the real system settings. – martineau Jun 27 '16 at 01:08

1 Answers1

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.

ands
  • 1,926
  • 16
  • 27