python實(shí)現(xiàn)windows壁紙定期更換功能
本文定期更換windows壁紙的python程序,很簡單,屬于自己寫著玩的那種,不提供完美的壁紙切換解決方案。
安裝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中看見。
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')
效果:
接下來,我們設(shè)定每隔一個(gè)小時(shí)換一次壁紙:
我的圖庫中只有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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python?Pygame實(shí)戰(zhàn)之五款童年經(jīng)典游戲合集
本文為大家總結(jié)了五款利用Python+Pygame實(shí)現(xiàn)的童年經(jīng)典游戲:推箱子、滑雪、八分音符醬、保衛(wèi)蘿卜和飛機(jī)大戰(zhàn),快跟隨小編一起學(xué)習(xí)一下2022-04-04Python?matplotlib實(shí)現(xiàn)多子圖布局
多子圖布局是指在一個(gè)圖像中同時(shí)顯示多個(gè)子圖,每個(gè)子圖可以是獨(dú)立的圖形或者是相互關(guān)聯(lián)的圖形,下面我們就來了解下matplotlib是如何實(shí)現(xiàn)多子圖布局的吧2023-12-12python 讀寫txt文件 json文件的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猵ython 讀寫、創(chuàng)建 文件的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10Python中的復(fù)雜數(shù)據(jù)類型(list、tuple)
這篇文章介紹了Python中的復(fù)雜數(shù)據(jù)類型(list、tuple),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05Python周期任務(wù)神器之Schedule模塊使用詳解
這篇文章主要為大家詳細(xì)介紹了Python中的周期任務(wù)神器—Schedule模塊的安裝和初級(jí)、進(jìn)階使用方法,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-04-04numpy中的高維數(shù)組轉(zhuǎn)置實(shí)例
下面小編就為大家分享一篇numpy中的高維數(shù)組轉(zhuǎn)置實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python list列表中刪除多個(gè)重復(fù)元素操作示例
這篇文章主要介紹了Python list列表中刪除多個(gè)重復(fù)元素操作,結(jié)合實(shí)例形式分析了Python刪除list列表重復(fù)元素的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-02-02Python中處理unchecked未捕獲異常實(shí)例
這篇文章主要介紹了Python中處理unchecked未捕獲異常實(shí)例,本文講解使用回調(diào)或者是鉤子來處理unchecked異常,需要的朋友可以參考下2015-01-01