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


Posted in Python onJanuary 21, 2019

本文定期更换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 -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

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

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:

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壁纸定期更换功能

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壁纸定期更换功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
用python代码将tiff图片存储到jpg的方法
Dec 04 Python
python 2.7 检测一个网页是否能正常访问的方法
Dec 26 Python
python SVM 线性分类模型的实现
Jul 19 Python
详解Python中的format格式化函数的使用方法
Nov 20 Python
Python Pandas 转换unix时间戳方式
Dec 07 Python
python3 常见解密加密算法实例分析【base64、MD5等】
Dec 19 Python
PyTorch实现AlexNet示例
Jan 14 Python
详解Python3中的 input() 函数
Mar 18 Python
PyCharm设置Ipython交互环境和宏快捷键进行数据分析图文详解
Apr 23 Python
python图片合成的示例
Nov 09 Python
Python脚本调试工具安装过程
Jan 11 Python
python 批量压缩图片的脚本
Jun 02 Python
PyQt5+requests实现车票查询工具
Jan 21 #Python
Python设计模式之策略模式实例详解
Jan 21 #Python
Python设计模式之装饰模式实例详解
Jan 21 #Python
python利用Tesseract识别验证码的方法示例
Jan 21 #Python
对python过滤器和lambda函数的用法详解
Jan 21 #Python
利用Python正则表达式过滤敏感词的方法
Jan 21 #Python
Python 实现王者荣耀中的敏感词过滤示例
Jan 21 #Python
You might like
php 什么是PEAR?(第二篇)
2009/03/19 PHP
CodeIgniter上传图片成功的全部过程分享
2013/08/12 PHP
PHP连接SQLServer2005方法及代码
2013/12/26 PHP
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
2014/06/04 PHP
PHP的PDO错误与错误处理
2019/01/27 PHP
jQuery操作Select选择的Text和Value(获取/设置/添加/删除)
2013/03/06 Javascript
高效的获取当前元素是父元素的第几个子元素
2013/10/15 Javascript
JavaScript事件委托用法分析
2015/01/24 Javascript
jQuery插入节点和移动节点用法示例(insertAfter、insertBefore方法)
2016/09/08 Javascript
jquery遍历标签中自定义的属性方法
2016/09/17 Javascript
使用vue实现点击按钮滑出面板的实现代码
2017/01/10 Javascript
AngularJS实现路由实例
2017/02/12 Javascript
微信浏览器禁止页面下拉查看网址实例详解
2017/06/28 Javascript
vue实现的双向数据绑定操作示例
2018/12/04 Javascript
JavaScript中条件语句的优化技巧总结
2020/12/04 Javascript
python中迭代器(iterator)用法实例分析
2015/04/29 Python
用python结合jieba和wordcloud实现词云效果
2017/09/05 Python
对pycharm代码整体左移和右移缩进快捷键的介绍
2018/07/16 Python
nginx黑名单和django限速,最简单的防恶意请求方法分享
2019/08/09 Python
python中def是做什么的
2020/06/10 Python
python 读取.nii格式图像实例
2020/07/01 Python
Expedia印度尼西亚站:预订酒店、廉价航班和度假套餐
2018/01/31 全球购物
企业项目策划书
2014/01/11 职场文书
《桃花心木》教学反思
2014/02/17 职场文书
个人租房协议书
2014/04/09 职场文书
爱岗敬业演讲稿
2014/05/05 职场文书
维修工先进事迹
2014/05/29 职场文书
岗位职责说明书模板
2014/07/30 职场文书
要账委托书范本
2014/09/15 职场文书
2014年学生会主席工作总结
2014/11/07 职场文书
毕业生见习报告总结
2014/11/08 职场文书
2014年工会工作总结
2014/11/12 职场文书
佛光寺导游词
2015/02/10 职场文书
地球上的星星观后感
2015/06/02 职场文书
《小摄影师》教学反思
2016/02/18 职场文书
nginx中proxy_pass各种用法详解
2021/11/07 Servers