Home > Blockchain >  changing the wallpaper in python program works when the program is ran directly. but doesn't wo
changing the wallpaper in python program works when the program is ran directly. but doesn't wo

Time:12-29

I've been wanting to make a program that immediatly sets my wallpaper to 1 picture, waits 10 seconds, then sets it to the other.

The program works just fine when running it from command prompt, or using the python interpreter. But as a service, it doesn't work.

I have used nssm (Non-Sucking Service Manager) to turn the script into a service.

Here's the wallpaper changing part of the code:

def change_wallpaper(filename):
    # Load the user32 library.
    user32 = ctypes.WinDLL('user32')

    # Set the wallpaper.
    result = user32.SystemParametersInfoW(
        win32con.SPI_SETDESKWALLPAPER, 0, filename, win32con.SPIF_UPDATEINIFILE
    )

    # Check the result.
    if not result:
        print("Failed to set wallpaper.")
    else:
        print("Successfully set wallpaper.")

I captured the I/O to a log file to find out if it had worked or not and it said "Failed to set wallpaper".

So.. I'm kind of stuck. Thanks to anyone who can help. :)


What I was expecting

I was expecting it to change the wallpaper, then after 10 seconds change it to another.

What actually happened

It reported that changing the wallpaper failed.

CodePudding user response:

Heads up, this is not my code. I've used a python wallpaper manager called superpaper before, and...it's a python wallpaper manager. The first rule of engineering is to not reinvent the wheel. Here is their project: Logon Settings

Equivalent commands:

nssm set UT2003 ObjectName LocalSystem nssm set UT2003 Type SERVICE_WIN32_OWN_PROCESS Refer to the command line usage documentation for details on configuring an account and password on the command line. If you need to configure a blank password you must use the command line.

Additionally, it may be related to this question: Change wallpaper with service

Personally, I would just choose not to make it a Windows Service, and instead opt for a tray service or background CLI application. You can just make a shortcut in a startup folder that will call python with the arguments to start your python script.

  • Related