• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python设置windows桌面壁纸的实现代码

python设置windows桌面壁纸的实现代码

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了windows桌面壁纸,windows桌面壁纸高清,windows7桌面壁纸,windows桌面壁纸下载,windows桌面壁纸大全等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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 + "\\%s.jpg" % datetime.date.today()
        f = open(path,"wb")
        f.write(img)
        f.close()
        setWallPaper(path)

setWallpaperOfToday()
print 'Wallpaper set ok!'
</div>
其中的httpwrapper是我写的一个http访问的封装:

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')
  """
  def __init__(self, url, data=None, method='GET', auth=None, user=None, password=None, cookie = None, **header):
    """
    url: 请求的url,不能为空
    date: 需要post的内容,必须是字典
    method: Get 或者 Post,默认为Get
    auth: 'base' 或者 'cookie'
    user: 用于base认证的用户名
    password: 用于base认证的密码
    cookie: 请求附带的cookie,一般用于登录后的认证
    其他头信息:
    e.g. referer='www.sina.com.cn'
    """

    self.url = url
    self.data = data
    self.method = method
    self.auth = auth
    self.user = user
    self.password = password
    self.cookie = cookie

    if 'referer' in header:
        self.referer = header[referer]
    else:
        self.referer = None

    if 'user-agent' in header:
        self.user_agent = header[user-agent]
    else:
## self.user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0'
        self.user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16'

    self.__SetupRequest()
    self.__SendRequest()

  def __SetupRequest(self):

    if self.url is None or self.url == '':
        raise 'url 不能为空!'

    #访问方式设置
    if self.method.lower() == 'post':
        self.Req = urllib2.Request(self.url, urllib.urlencode(self.data))

    elif self.method.lower() == 'get':
        if self.data == None:
            self.Req = urllib2.Request(self.url)
        else:
            self.Req = urllib2.Request(self.url + '?' + urllib.urlencode(self.data))

    #设置认证信息
    if self.auth == 'base':
        if self.user == None or self.password == None:
            raise 'The user or password was not given!'
        else:
            auth_info = base64.encodestring(self.user + ':' + self.password).replace('\n','')
            auth_info = 'Basic ' + auth_info
            self.Req.add_header("Authorization", auth_info)

    elif self.auth == 'cookie':
        if self.cookie == None:
            raise 'The cookie was not given!'
        else:
            self.Req.add_header("Cookie", self.cookie)


    if self.referer:
        self.Req.add_header('referer', self.referer)
    if self.user_agent:
        self.Req.add_header('user-agent', self.user_agent)


  def __SendRequest(self):

    try:
      self.Res = urllib2.urlopen(self.Req)
      self.source = self.Res.read()
      self.code = self.Res.getcode()
      self.head_dict = self.Res.info().dict
      self.Res.close()
    except:
      print "Error: HttpWrapper=>_SendRequest ", sys.exc_info()[1]


  def GetResponseCode(self):
  &nb

您可能想查找下面的文章:

  • Python实现设置windows桌面壁纸代码分享
  • Python实现设置windows桌面壁纸代码分享
  • python设置windows桌面壁纸的实现代码
  • python设置windows桌面壁纸的实现代码

相关文章

  • Python程序员开发中常犯的10个错误
  • TensorFlow绘制loss/accuracy曲线的实例
  • Python常见格式化字符串方法小结【百分号与format方法】
  • Python文件夹与文件的操作实现代码
  • python复制与引用用法分析
  • python使用post提交数据到远程url的方法
  • python类装饰器用法实例
  • python读写ini配置文件方法实例分析
  • 在主机商的共享服务器上部署Django站点的方法
  • 利用Python学习RabbitMQ消息队列

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 简单的Apache+FastCGI+Django配置指南
    • python通过shutil实现快速文件复制的方法
    • python中pass语句用法实例分析
    • 教你如何将 Sublime 3 打造成 Python/Django IDE开发利器
    • Python常用算法学习基础教程
    • Python实现约瑟夫环问题的方法
    • Python使用稀疏矩阵节省内存实例
    • 在Python的web框架中配置app的教程
    • Python基于sftp及rsa密匙实现远程拷贝文件的方法
    • Python的几个高级语法概念浅析(lambda表达式闭包装饰器)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有