Change wallpaper in Python for user while being system

落花浮王杯 提交于 2019-12-06 05:00:32

How about creating a value key in the registry at:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

This will change the background when ever the user login.

To try it, write this script, name it for example SetDesktopBackground.py, any where you like:

#!python

from ctypes import *
from os import path

SPI_SETDESKWALLPAPER = 0x14
SPIF_UPDATEINIFILE   = 0x1

lpszImage = path.join(path.dirname(path.realpath(__file__)), 'your_image.jpg')

SystemParametersInfo = windll.user32.SystemParametersInfoA

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, lpszImage, SPIF_UPDATEINIFILE)

Dont forgot to put some image, your_image.jpg, in the same directory. Then open the registery editor:

Start > Search > type regedit.exe

Then go to the path:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Right click and choose New > String Value and type any name you like for this value.

Right click on this new value and choose Modify, in the Data Value field write:

"C:\Python26\pythonw.exe" "C:\Path\To\SetDesktopBackground.py"

To test it, logout and login again. The background should change when ever this user login.

That was the manual way to do it, you can use _winreg in your application to create the value during the installation:

#!python

from _winreg import *
from sys import executable
from os import path

subkey  = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run'
script  = 'C:\\Path\\To\\SetDesktopBackground.py'
pythonw = path.join(path.dirname(executable), 'pythonw.exe')

hKey = OpenKey(HKEY_CURRENT_USER, subkey, 0, KEY_SET_VALUE)

SetValueEx(hKey, 'MyApp', 0, REG_SZ, '"{0}" "{1}"'.format(pythonw, script))

CloseKey(hKey)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!