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批量修改文件名的实现代码
Sep 01 Python
Python 编码Basic Auth使用方法简单实例
May 25 Python
Python微信库:itchat的用法详解
Aug 14 Python
Python  unittest单元测试框架的使用
Sep 08 Python
详解爬虫被封的问题
Apr 23 Python
使用Python实现毫秒级抢单功能
Jun 06 Python
用Python调用win命令行提高工作效率的实例
Aug 14 Python
python__new__内置静态方法使用解析
Jan 07 Python
Python3.9又更新了:dict内置新功能
Feb 28 Python
基于python实现检索标记敏感词并输出
May 07 Python
python线程池 ThreadPoolExecutor 的用法示例
Oct 10 Python
Python 删除List元素的三种方法remove、pop、del
Nov 16 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 购物车完整实现代码
2014/06/05 PHP
ThinkPHP表单自动提交验证实例教程
2014/07/18 PHP
php实现将数据做成json的格式给前端使用
2018/08/21 PHP
图片之间的切换
2006/06/26 Javascript
jquery 锁定弹出层实现代码
2010/02/23 Javascript
dtree 网页树状菜单及传递对象集合到js内,动态生成节点
2012/04/14 Javascript
Js控制弹窗实现在任意分辨率下居中显示
2013/08/01 Javascript
js进行表单验证实例分析
2015/02/10 Javascript
jQuery实现dialog设置focus焦点的方法
2015/06/10 Javascript
JS给Textarea文本框添加行号的方法
2015/08/20 Javascript
js钢琴按钮波浪式图片排列效果代码分享
2015/08/26 Javascript
基于JavaScript实现点击页面任何位置返回
2016/08/31 Javascript
jquery实现一个全局计时器(商城可用)
2017/06/30 jQuery
vue-cli 3.0 自定义vue.config.js文件,多页构建的方法
2018/09/19 Javascript
详解使用uni-app开发微信小程序之登录模块
2019/05/09 Javascript
浅谈layer的Icon样式以及一些常用的layer窗口使用方法
2019/09/11 Javascript
node.js中事件触发器events的使用方法实例分析
2019/11/23 Javascript
JavaScript实现简单的弹窗效果
2020/05/19 Javascript
Python对两个有序列表进行合并和排序的例子
2014/06/13 Python
Python显示进度条的方法
2014/09/20 Python
Python 创建子进程模块subprocess详解
2015/04/08 Python
python输出当前目录下index.html文件路径的方法
2015/04/28 Python
PyQt QCombobox设置行高的方法
2019/06/20 Python
使用Matplotlib 绘制精美的数学图形例子
2019/12/13 Python
关于Python turtle库使用时坐标的确定方法
2020/03/19 Python
python + selenium 刷B站播放量的实例代码
2020/06/12 Python
Pamela Love官网:纽约设计师Pamela Love的精美、时尚和穿孔珠宝
2020/10/19 全球购物
opencv实现图像几何变换
2021/03/24 Python
涉外文秘个人求职的自我评价
2013/10/07 职场文书
计算机个人求职信范例
2014/01/24 职场文书
致地震灾区的慰问信
2015/03/23 职场文书
人间正道是沧桑观后感
2015/06/15 职场文书
2016重阳节红领巾广播稿
2015/12/18 职场文书
2019年房屋委托租赁合同范本(通用版)!
2019/07/17 职场文书
Golang中interface{}转为数组的操作
2021/04/30 Golang
MySQL慢查询优化解决问题
2022/03/17 MySQL