python 更换windows壁纸(简单)

网友投稿 1305 2022-08-27

python 更换windows壁纸(简单)

python 更换windows壁纸(简单)

本文定期更换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的目录)

C:\Python27\Scripts>python pywin32_postinstall.py -installCopied pythoncom27.dll to C:\Windows\SysWOW64\pythoncom27.dllCopied pythoncomloader27.dll to C:\Windows\SysWOW64\pythoncomloader27.dllCopied pywintypes27.dll to C:\Windows\SysWOW64\pywintypes27.dllRegistered: Python.InterpreterRegistered: Python.DictionaryRegistered: 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 menuShortcut for Pythonwin createdShortcut to documentation createdThe pywin32 extensions were successfully installed.

这样,pywin32就完成了安装。

安装PIL

PIL即是Python Image Lib。 在网上-PIL: ​​​ 注:如果要使用pip安装,那么命令行中输入的不是pip,而是pip2.7,如下:

C:\Python27\Scripts>pip2.7 installYou must give at least one requirement to install (see "pip help install")

关键函数

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

SystemParametersInfo(Action, Param, WinIni)Queries or sets system-wide parameters. This function can also update the user profile while setting a parameter.ParametersundefinedAction : int System parameter to query or set, one of the SPI_GET* or SPI_SET* constantsParam=None : object depends on action to be takenWinIni=0 : int Flags specifying whether change should be permanent, and if all windows should be notified of change. Combination of

win32api.RegOpenKeyEx

PyHKEY = RegOpenKeyEx(key, subKey, reserved , sam )Opens the specified key.Parametersundefinedkey : PyHKEY/int An already open key, or any one of the following win32con constants:HKEY_CLASSES_ROOTHKEY_CURRENT_USERHKEY_LOCAL_MACHINEHKEY_USERSsubKey : 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

程序

接下来就是coding: set.py:

import Imageimport win32api, win32gui, win32condef 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')

效果:

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

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

import Imageimport win32api, win32gui, win32conimport timedef 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 = 0while 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)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:2014最流行的编程语言
下一篇:QT 窗体上放GIF动态图
相关文章

 发表评论

暂时没有评论,来抢沙发吧~