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线程、进程和协程详解
Jul 19 Python
python中星号变量的几种特殊用法
Sep 07 Python
详解Django中间件执行顺序
Jul 16 Python
numpy中loadtxt 的用法详解
Aug 03 Python
详解python读取image
Apr 03 Python
python输入多行字符串的方法总结
Jul 02 Python
如何利用python给图片添加半透明水印
Sep 06 Python
Python安装tar.gz格式文件方法详解
Jan 19 Python
浅谈PyTorch的可重复性问题(如何使实验结果可复现)
Feb 20 Python
在python中使用pymysql往mysql数据库中插入(insert)数据实例
Mar 02 Python
浅析Python 多行匹配模式
Jul 24 Python
python中if和elif的区别介绍
Nov 07 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
解析百度搜索结果link?url=参数分析 (全)
2012/10/09 PHP
完美解决phpdoc导出文档中@package的warning及Error的错误
2016/05/17 PHP
JavaScript使用prototype定义对象类型
2007/02/07 Javascript
javascript function调用时的参数检测常用办法
2010/02/26 Javascript
关于jquery css的使用介绍
2013/04/18 Javascript
js获取GridView中行数据的两种方法 分享
2013/07/13 Javascript
JS实现下拉菜单赋值到文本框的方法
2015/08/18 Javascript
jQuery实现鼠标滑过链接控制图片的滑动展开与隐藏效果
2015/10/28 Javascript
用JavaScript来美化HTML的select标签的下拉列表效果
2015/11/17 Javascript
JavaScript实现跑马灯抽奖活动实例代码解析与优化(二)
2016/02/16 Javascript
js密码强度实时检测代码
2016/03/02 Javascript
JQuery Mobile实现导航栏和页脚
2016/03/09 Javascript
微信小程序 Storage API实例详解
2016/10/02 Javascript
微信小程序 条件渲染详解
2016/10/09 Javascript
vue分类筛选filter方法简单实例
2017/03/30 Javascript
js实现本地时间同步功能
2017/08/26 Javascript
vue监听scroll的坑的解决方法
2017/09/07 Javascript
jquery中ajax请求后台数据成功后既不执行success也不执行error的完美解决方法
2017/12/24 jQuery
nodejs中express入门和基础知识点学习
2018/09/13 NodeJs
JS如何在不同平台实现多语言方式
2020/07/16 Javascript
使用Python将数组的元素导出到变量中(unpacking)
2016/10/27 Python
python增加矩阵维度的实例讲解
2018/04/04 Python
PySide和PyQt加载ui文件的两种方法
2019/02/27 Python
GitHub 热门:Python 算法大全,Star 超过 2 万
2019/04/29 Python
python使用for...else跳出双层嵌套循环的方法实例
2020/05/17 Python
使用Keras画神经网络准确性图教程
2020/06/15 Python
python3中TQDM库安装及使用详解
2020/11/18 Python
任意存:BOXFUL
2018/05/21 全球购物
教育科研先进个人材料
2014/01/26 职场文书
20年同学聚会感言
2014/02/03 职场文书
幼儿园大班毕业感言
2014/02/06 职场文书
自动一体化专业求职信
2014/03/15 职场文书
开服装店计划书
2014/08/15 职场文书
考研英语复习计划
2015/01/19 职场文书
深入浅析React中diff算法
2021/05/19 Javascript