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中IPYTHON入门实例
May 11 Python
Python中用sleep()方法操作时间的教程
May 22 Python
Python打造出适合自己的定制化Eclipse IDE
Mar 02 Python
Django框架的使用教程路由请求响应的方法
Jul 03 Python
Python3爬虫学习之将爬取的信息保存到本地的方法详解
Dec 12 Python
Python3解释器知识点总结
Feb 19 Python
python实现发送form-data数据的方法详解
Sep 27 Python
python框架flask入门之路由及简单实现方法
Jun 07 Python
keras实现多GPU或指定GPU的使用介绍
Jun 17 Python
python openCV实现摄像头获取人脸图片
Aug 20 Python
详解Python中如何将数据存储为json格式的文件
Nov 18 Python
Django解决frame拒绝问题的方法
Dec 18 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/02/27 PHP
WordPress中获取所使用的模板的页面ID的简单方法
2015/12/31 PHP
Laravel学习教程之本地化模块
2017/08/18 PHP
Yii2压缩PHP中模板代码的输出问题
2018/08/28 PHP
实例讲解PHP表单验证功能
2019/02/15 PHP
BOOM vs RR BO5 第一场 2.14
2021/03/10 DOTA
csdn 批量接受好友邀请
2009/02/19 Javascript
基于jquery的loading效果实现代码
2010/11/05 Javascript
推荐40个非常优秀的jQuery插件和教程【系列三】
2011/11/09 Javascript
ASP.NET MVC中EasyUI的datagrid跨域调用实现代码
2012/03/14 Javascript
js和css写一个可以自动隐藏的悬浮框
2014/03/05 Javascript
神奇!js+CSS+DIV实现文字颜色渐变效果
2016/03/16 Javascript
深入解析桶排序算法及Node.js上JavaScript的代码实现
2016/07/06 Javascript
JS实现图文并茂的tab选项卡效果示例【附demo源码下载】
2016/09/21 Javascript
Ionic + Angular.js实现图片轮播的方法示例
2017/05/21 Javascript
Vue学习之路之登录注册实例代码
2017/07/06 Javascript
纯JS开发baguetteBox.js响应式画廊插件
2020/06/28 Javascript
[01:26]神话结束了,却也刚刚开始——DOTA2新英雄玛尔斯驾临战场
2019/03/10 DOTA
[01:34:42]NAVI vs EG 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
Eclipse + Python 的安装与配置流程
2013/03/05 Python
初步解析Python下的多进程编程
2015/04/28 Python
浅谈Python中的私有变量
2018/02/28 Python
对numpy中数组元素的统一赋值实例
2018/04/04 Python
python实现QQ空间自动点赞功能
2019/04/09 Python
python爬虫解决验证码的思路及示例
2019/08/01 Python
Python小程序 控制鼠标循环点击代码实例
2019/10/08 Python
python隐藏类中属性的3种实现方法
2019/12/19 Python
Python3.x+pyqtgraph实现数据可视化教程
2020/03/14 Python
利用Python发送邮件或发带附件的邮件
2020/11/12 Python
css3打造一款漂亮的卡哇伊按钮
2013/03/20 HTML / CSS
水果花束:Fruit Bouquets
2017/12/20 全球购物
Book Depository欧盟:一家领先的国际图书零售商
2019/05/21 全球购物
皮肤科医师岗位职责
2013/12/04 职场文书
业务员的岗位职责
2014/03/15 职场文书
分公司总经理岗位职责
2014/08/03 职场文书
golang 如何通过反射创建新对象
2021/04/28 Golang