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 创建子进程模块subprocess详解
Apr 08 Python
python实现根据ip地址反向查找主机名称的方法
Apr 29 Python
深入解析Python中的WSGI接口
May 11 Python
Python正则表达式使用范例分享
Dec 04 Python
从零开始学Python第八周:详解网络编程基础(socket)
Dec 14 Python
使用python编写监听端
Apr 12 Python
Django重装mysql后启动报错:No module named ‘MySQLdb’的解决方法
Apr 22 Python
Python字符串处理的8招秘籍(小结)
Aug 13 Python
下载与当前Chrome对应的chromedriver.exe(用于python+selenium)
Jan 14 Python
python 计算概率密度、累计分布、逆函数的例子
Feb 25 Python
如何基于pandas读取csv后合并两个股票
Sep 25 Python
python 多态 协议 鸭子类型详解
Nov 27 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获取远程图片并把它保存到本地的代码
2008/04/07 PHP
php截取字符串函数分享
2015/02/02 PHP
WordPress开发中短代码的实现及相关函数使用技巧
2016/01/05 PHP
Centos 6.5系统下编译安装PHP 7.0.13的方法
2016/12/19 PHP
Javascript 读后台cookie代码
2008/09/15 Javascript
js几个验证函数代码
2010/03/25 Javascript
Notify - 基于jquery的消息通知插件
2011/10/18 Javascript
JS动态获取当前时间,并写到特定的区域
2013/05/03 Javascript
JS滚轮事件onmousewheel使用介绍
2013/11/01 Javascript
NODE.JS加密模块CRYPTO常用方法介绍
2014/06/05 Javascript
原生的html元素选择器类似jquery选择器
2014/10/15 Javascript
javacript使用break内层跳出外层循环分析
2015/01/12 Javascript
JavaScript清空数组元素的两种方法简单比较
2015/07/10 Javascript
js实现文件上传表单域美化特效
2015/11/02 Javascript
详解微信小程序开发—你期待的分享功能来了,微信小程序序新增5大功能
2016/12/23 Javascript
微信小程序 两种滑动方式(横向滑动,竖向滑动)详细及实例代码
2017/01/13 Javascript
JavaScript正则表达式替换字符串中图片地址(img src)的方法
2017/01/13 Javascript
详解vue.js之props传递参数
2017/12/12 Javascript
微信小程序中使用自定义图标(阿里icon)的方法
2018/08/20 Javascript
jQuery选择器之基本过滤选择器用法实例分析
2019/02/19 jQuery
微信小程序新手教程之启动页的重要性
2019/03/03 Javascript
JS实现简单贪吃蛇小游戏
2020/10/28 Javascript
[31:00]2014 DOTA2华西杯精英邀请赛5 24 NewBee VS iG
2014/05/25 DOTA
Python实现基于权重的随机数2种方法
2015/04/28 Python
Python中函数的参数传递与可变长参数介绍
2015/06/30 Python
树莓派4B+opencv4+python 打开摄像头的实现方法
2019/10/18 Python
python向图片里添加文字
2019/11/26 Python
使用python-Jenkins批量创建及修改jobs操作
2020/05/12 Python
若干个Java基础面试题
2015/05/19 面试题
历史学专业毕业生求职信
2013/09/27 职场文书
经典禁毒标语
2014/06/16 职场文书
银行贷款委托书范本
2014/10/11 职场文书
民事答辩状范本
2015/05/21 职场文书
python将图片转为矢量图的方法步骤
2021/03/30 Python
MySQL 角色(role)功能介绍
2021/04/24 MySQL
mysql sum(if())和count(if())的用法说明
2022/01/18 MySQL