国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python設置windows桌面壁紙的實現代碼

2020-02-23 04:50:10
字體:
來源:轉載
供稿:網友

代碼如下:
# -*- coding: UTF-8 -*-

from __future__ import unicode_literals
import Image
import datetime
import win32gui,win32con,win32api
import re
from HttpWrapper import SendRequest

StoreFolder = "c://dayImage"

def setWallpaperFromBMP(imagepath):
    k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel//Desktop",0,win32con.KEY_SET_VALUE)
    win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸適應桌面,0桌面居中
    win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)

def setWallPaper(imagePath):
    """
    Given a path to an image, convert it to bmp and set it as wallpaper
    """
    bmpImage = Image.open(imagePath)
    newPath = StoreFolder + '//mywallpaper.bmp'
    bmpImage.save(newPath, "BMP")
    setWallpaperFromBMP(newPath)

def getPicture():
    url = "http://photography.nationalgeographic.com/photography/photo-of-the-day/"
    h = SendRequest(url)
    if h.GetSource():
        r = re.findall('<div class="download_link"><a href="(.*?)">Download',h.GetSource())
        if r:
            return SendRequest(r[0]).GetSource()
        else:
            print "解析圖片地址出錯,請檢查正則表達式是否正確"
            return None


def setWallpaperOfToday():
    img = getPicture()
    if img:
        path = StoreFolder + "http://%s.jpg" % datetime.date.today()
        f = open(path,"wb")
        f.write(img)
        f.close()
        setWallPaper(path)

setWallpaperOfToday()
print 'Wallpaper set ok!'

其中的httpwrapper是我寫的一個http訪問的封裝:
代碼如下:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name: 對http訪問的封裝
#
# Author: qianlifeng
#
# Created: 10-02-2012
#-------------------------------------------------------------------------------

import base64
import urllib
import urllib2
import time
import re
import sys

class SendRequest:
  """
  網頁請求增強類
  SendRequest('http://xxx.com',data=dict, type='POST', auth='base',user='xxx', password='xxx')

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 行唐县| 林口县| 台州市| 滦南县| 武城县| 宣威市| 南木林县| 涞源县| 甘洛县| 嘉兴市| 贞丰县| 新竹县| 普陀区| 关岭| 丰原市| 大荔县| 隆德县| 洛南县| 尖扎县| 祁东县| 长葛市| 阳城县| 昭通市| 德令哈市| 墨脱县| 黄骅市| 福海县| 若尔盖县| 固原市| 定边县| 吴川市| 秭归县| 西华县| 丹阳市| 抚松县| 清丰县| 健康| 巩义市| 宾川县| 额济纳旗| 汉中市|