在系统中为用户更改Python中的墙纸 [英] Change wallpaper in Python for user while being system

查看:72
本文介绍了在系统中为用户更改Python中的墙纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是在Windows中更改桌面墙纸。
为此,我使用以下代码:

what I am trying to do is change the desktop wallpaper in windows. To do that, I use the following code:

import ctypes
import Image

pathToBmp = "PATH TO BMP FILE"
SPI_SETDESKWALLPAPER = 20  
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pathToBmp , 0) 

这在我运行.py文件时有效,当我使用py2exe对其进行转换并在当前用户下运行exe时该功能有效,但是当我运行exe作为SYSTEM,当前用户背景不会更改。

this works when I run the .py file, this works when I convert it using py2exe and run the exe under the current user, but when I run the exe as SYSTEM, the current user background does not change.

这当然是可以预期的。但是我不知道该怎么解决。

This ofcourse was to be expected. But I don't know how to solve it.

顺便说一句,您的解决方案是否更改了当前用户背景或所有用户背景都没有关系。

By the way, it does not matter if any of your solutions changes the current user background or all the users' backgrounds.

谢谢您的时间。

推荐答案

如何创建注册表中的值键:

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.

要尝试使用此脚本,请编写该脚本,并将其命名为 SetDesktopBackground.py ,在任何您喜欢的位置:

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)

不要忘记放一些图像, your_image.jpg ,在同一目录中。然后打开注册编辑器:

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

Start > Search > type regedit.exe

然后转到路径:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

右键单击并选择 New>字符串值,然后输入您喜欢的任何名称作为该值。

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.

这是手动操作,您可以使用 _ winreg 在安装过程中创建值:

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)

这篇关于在系统中为用户更改Python中的墙纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆