python實(shí)現(xiàn)windows壁紙定期更換功能
本文定期更換windows壁紙的python程序,很簡(jiǎn)單,屬于自己寫著玩的那種,不提供完美的壁紙切換解決方案。
安裝pywin32 extensions
安裝python2.7后,然后管理員的方式運(yùn)行cmd,進(jìn)入python的scripts目錄,我的是
C:\Python27\Scripts
cd /d C:\Python27\Scripts
然后敲入:python pywin32_postinstall.py -install(先確保在環(huán)境變量PATH中設(shè)置好了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。
在網(wǎng)上下載PIL: http://www.pythonware.com/products/pil/。我下載的是PIL-1.1.7.win32-py2.7.exe,雙擊運(yùn)行即可。
注:如果要使用pip安裝,那么命令行中輸入的不是pip,而是pip2.7,如下:
C:\Python27\Scripts>pip2.7 install
You must give at least one requirement to install (see "pip help install")
關(guān)鍵函數(shù)
下面的函數(shù)幫助信息都能在PyWin32.chm中看見(jiàn)。
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
程序
接下來(lái)就是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')
效果:

接下來(lái),我們?cè)O(shè)定每隔一個(gè)小時(shí)換一次壁紙:
我的圖庫(kù)中只有5張圖片,所以顯示圖片的標(biāo)志只能在[1 - 5]中循環(huán)了。

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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
wxpython中Textctrl回車事件無(wú)效的解決方法
這篇文章主要介紹了wxpython中Textctrl回車事件無(wú)效的解決方法,較為詳細(xì)的分析了TextCtrl支持的事件類型,并給出了TextCtrl綁定回車事件的相應(yīng)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
python使用scapy模塊實(shí)現(xiàn)ping掃描的過(guò)程詳解
這篇文章主要介紹了python使用scapy模塊實(shí)現(xiàn)ping掃描的過(guò)程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
安裝pyinstaller遇到的各種問(wèn)題(小結(jié))
這篇文章主要介紹了安裝pyinstaller遇到的各種問(wèn)題(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
在dataframe兩列日期相減并且得到具體的月數(shù)實(shí)例
今天小編就為大家分享一篇在dataframe兩列日期相減并且得到具體的月數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python辦公自動(dòng)化之操控遠(yuǎn)程桌面和文件版本控制
這篇文章主要為大家詳細(xì)介紹了Python辦公自動(dòng)化中操控遠(yuǎn)程桌面和文件版本控制的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-01-01
詳解python uiautomator2 watcher的使用方法
這篇文章主要介紹了python uiautomator2 watcher的使用方法,該方是基于uiautomator2如下版本進(jìn)行驗(yàn)證,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-09-09
PyQt5?QLineEdit校驗(yàn)器限制輸入實(shí)例代碼
QLineEdit類是一個(gè)單行文本控件,可輸入單行字符串,可以設(shè)置回顯模式(Echomode)和掩碼模式,下面這篇文章主要給大家介紹了關(guān)于PyQt5?QLineEdit校驗(yàn)器限制輸入的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05

