python实现windows壁纸定期更换功能

时间:2022-06-15 07:18:15

本文定期更换windows壁纸python程序,很简单,属于自己写着玩的那种,不提供完美的壁纸切换解决方案。

安装pywin32 extensions

安装python2.7后,然后管理员的方式运行cmd,进入python的scripts目录,我的是

c:\python27\scripts
cd /d c:\python27\scripts

然后敲入:python pywin32_postinstall.py -install(先确保在环境变量path中设置好了python.exe的目录)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
c:\python27\scripts>python pywin32_postinstall.py -install
copied pythoncom27.dll to c:\windows\syswow64\pythoncom27.dll
copied pythoncomloader27.dll to c:\windows\syswow64\pythoncomloader27.dll
copied pywintypes27.dll to c:\windows\syswow64\pywintypes27.dll
registered: python.interpreter
registered: python.dictionary
registered: python
-> software\python\pythoncore\2.7\help[none]=none
-> software\python\pythoncore\2.7\help\pythonwin reference[none]='c:\\python27\\
lib\\site-packages\\pywin32.chm'
pythonwin has been registered in context menu
shortcut for pythonwin created
shortcut to documentation created
the pywin32 extensions were successfully installed.

这样,pywin32就完成了安装。

安装pil

pil即是python image lib。
在网上下载pil: http://www.pythonware.com/products/pil/。我下载的是pil-1.1.7.win32-py2.7.exe,双击运行即可。
注:如果要使用pip安装,那么命令行中输入的不是pip,而是pip2.7,如下:

c:\python27\scripts>pip2.7 install
you must give at least one requirement to install (see "pip help install")

关键函数

下面的函数帮助信息都能在pywin32.chm中看见。
win32gui.systemparametersinfo

?
1
2
3
4
5
6
7
8
9
systemparametersinfo(action, param, winini)
queries or sets system-wide parameters. this function can also update the user profile while setting a parameter.
parametersundefined
action : int
 system parameter to query or set, one of the spi_get* or spi_set* constants
param=none : object
 depends on action to be taken
winini=0 : int
 flags specifying whether change should be permanent, and if all windows should be notified of change. combination of spif_updateinifile, spif_sendchange, spif_sendwininichange

win32api.regopenkeyex

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
pyhkey = regopenkeyex(key, subkey, reserved , sam )
opens the specified key.
parametersundefined
key : pyhkey/int
 an already open key, or any one of the following win32con constants:
hkey_classes_root
hkey_current_user
hkey_local_machine
hkey_users
subkey : string
 the name of a key that this method opens. this key must be a subkey of the key identified by the key parameter. if key is one of the predefined keys, subkey may be none. in that case, the handle returned is the same key handle passed in to the function.
reserved=0 : int
 reserved. must be zero.
sam=key_read : int
 specifies an access mask that describes the desired security access for the new key. this parameter can be a combination of the following win32con constants:
key_all_access
key_create_link
key_create_sub_key
key_enumerate_sub_keys
key_execute
key_notify
key_query_value
key_read
key_set_value
key_write

程序

接下来就是coding:
set.py:

?
1
2
3
4
5
6
7
8
9
10
11
12
import image
import win32api, win32gui, win32con
 
def setwallpaper(pic):
 # open register
 regkey = win32api.regopenkeyex(win32con.hkey_current_user,"control panel\\desktop",0,win32con.key_set_value)
 win32api.regsetvalueex(regkey,"wallpaperstyle", 0, win32con.reg_sz, "2")
 win32api.regsetvalueex(regkey, "tilewallpaper", 0, win32con.reg_sz, "0")
 # refresh screen
 win32gui.systemparametersinfo(win32con.spi_setdeskwallpaper,pic, win32con.spif_sendwininichange)
 
setwallpaper('e:\\backpics\\character5.jpg')

效果:

python实现windows壁纸定期更换功能

接下来,我们设定每隔一个小时换一次壁纸:

我的图库中只有5张图片,所以显示图片的标志只能在[1 - 5]中循环了。

python实现windows壁纸定期更换功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import image
import win32api, win32gui, win32con
import time
 
def setwallpaper(pic):
 # open register
 regkey = win32api.regopenkeyex(win32con.hkey_current_user,"control panel\\desktop",0,win32con.key_set_value)
 win32api.regsetvalueex(regkey,"wallpaperstyle", 0, win32con.reg_sz, "2")
 win32api.regsetvalueex(regkey, "tilewallpaper", 0, win32con.reg_sz, "0")
 # refresh screen
 win32gui.systemparametersinfo(win32con.spi_setdeskwallpaper,pic, win32con.spif_sendwininichange)
 
g_times = 0
while true:
 g_times = g_times+1
 g_times = g_times%5
 picdir = 'e:\\backpics\\character'
 picdir = picdir+str(g_times+1)+'.jpg'
 setwallpaper(picdir)
 time.sleep(60*60)

python实现windows壁纸定期更换功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/theArcticOcean/article/details/78239904