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在校内发人人网状态(人人网看状态)
Feb 19 Python
windows下python模拟鼠标点击和键盘输示例
Feb 28 Python
python获取指定路径下所有指定后缀文件的方法
May 26 Python
Python Paramiko模块的安装与使用详解
Nov 18 Python
详谈Python2.6和Python3.0中对除法操作的异同
Apr 28 Python
详解Python if-elif-else知识点
Jun 11 Python
python读取xlsx的方法
Dec 25 Python
python plotly绘制直方图实例详解
Jul 22 Python
Django Rest framework三种分页方式详解
Jul 26 Python
python实现图片插入文字
Nov 26 Python
Python类及获取对象属性方法解析
Jun 15 Python
opencv 图像加法与图像融合的实现代码
Jul 08 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 mcrypt可逆加密算法分析
2011/07/19 PHP
php之readdir函数用法实例
2014/11/13 PHP
PHP多线程编程之管道通信实例分析
2015/03/07 PHP
关于PHP内置的字符串处理函数详解
2017/02/04 PHP
Laravel多域名下字段验证的方法
2019/04/04 PHP
Extjs4中Form的使用之本地hiddenfield
2013/11/26 Javascript
JavaScript移除数组内重复元素的方法
2015/03/18 Javascript
JS截取与分割字符串常用技巧总结
2015/11/10 Javascript
js如何改变文章的字体大小
2016/01/08 Javascript
利用jQuery中的ajax分页实现代码
2016/02/25 Javascript
JavaScript中实现无缝滚动、分享到侧边栏实例代码
2016/04/06 Javascript
JS实现拖动滚动条评分的效果代码分享
2016/09/29 Javascript
微信小程序 自己制作小组件实例详解
2016/12/22 Javascript
微信小程序 Toast自定义实例详解
2017/01/20 Javascript
Javascript基础回顾之(二) js作用域
2017/01/31 Javascript
基于LayUI分页和LayUI laypage分页的使用示例
2017/08/02 Javascript
解决vue attr取不到属性值的问题
2018/09/18 Javascript
jQuery实现B2B网站后台管理系统侧导航
2020/07/08 jQuery
[01:00:11]DOTA2-DPC中国联赛 正赛 CDEC vs DLG BO3 第一场 2月7日
2021/03/11 DOTA
python开发之IDEL(Python GUI)的使用方法图文详解
2015/11/12 Python
详解如何使用Python编写vim插件
2017/11/28 Python
详解tensorflow载入数据的三种方式
2018/04/24 Python
python入门:这篇文章带你直接学会python
2018/09/14 Python
python模拟鼠标点击和键盘输入的操作
2019/08/04 Python
django 连接数据库 sqlite的例子
2019/08/14 Python
python爬虫 基于requests模块发起ajax的get请求实现解析
2019/08/20 Python
win7下 python3.6 安装opencv 和 opencv-contrib-python解决 cv2.xfeatures2d.SIFT_create() 的问题
2019/10/24 Python
Django框架静态文件处理、中间件、上传文件操作实例详解
2020/02/29 Python
Django中FilePathField字段的用法
2020/05/21 Python
Python字符串三种格式化输出
2020/09/17 Python
荷兰照明、灯具和配件网上商店:dmlights
2019/08/25 全球购物
英文版餐饮运营管理求职信
2013/11/06 职场文书
优秀员工年终发言演讲稿
2014/01/01 职场文书
董事长助理岗位职责
2014/02/18 职场文书
先进班组事迹材料
2014/12/25 职场文书
分布式Redis Cluster集群搭建与Redis基本用法
2022/02/24 Redis