2006年10月16日 星期一 10:25
大家好:
好久没有在此留言了,但一直关注着这里,看别人的交流受益匪浅。由于本人平时不使用python语言,所以自学速度很慢。
周末没有什么事情,所以写了一个小python例子,用于自动更改桌面壁纸。(看到同事使用番茄花园xp中有此功能,所以就像用python实现一下),由于本人对python的理解还不够,所以下面的代码中一定有很多不如人意的地方,希望大家多给指正,谢谢。
注此程序只在xp下运行过,用python 2.4.3 开发,代码如下:
import sys, os, time, win32gui# 也可以使用 ctypes 代替win32gui 同事恢复响应的注释语句即可
print '改变桌面程序,运行开始'
finalfile = [] #保存最后要显示的图片集和
file_type = ['bmp']#'jpg', 'gif', 'jpeg', 'dib', 'png' 设置文件类型
length = 0 #循环长度
pos = 0 #当前循环位置
if sys.platform[0:3] 'win': #系统判断
sys.exit("此程序只适用于windows")
dire = "C:/Documents and Settings/sinic/My Documents/My Pictures/zelda" #图片目录设置,请自行修改
if not os.path.exists(dire): #目录有效性判断
print "Not find dirction, please affirm your input!"
sys.exit("文件选择有误,运行停止")
list = os.listdir(dire) #提取目录中所有列表
for filename in list: #判断及组合有效文件 文件扩展名
if len(filename.split('.')) == 2: #消除目录判断
if filename.split('.') [1] in file_type: #扩展名判断
finalfile.append(filename)
if finalfile.__eq__([]): #是否有有效文件
sys.exit("没有图片文件,运行停止")
SPI_SETDESKWALLPAPER = 0x14 #查msdn中解释 20 是指设置壁纸
length = len(finalfile) #壁纸循环显示
while True:
if pos < length:
pass
#ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, dire+'/'+finalfile[pos] , 0)
win32gui.SystemParametersInfo(SPI_SETDESKWALLPAPER, dire+'/'+finalfile[pos] , 0)
pos = pos+1
time.sleep(60) #60秒 = 1分钟 更换一次
else:
pass
pos = 0 #回到显示图片集和中的第一张图片
此代码有很多不足之处,功能部分它只是基本实现桌面替换功能。 其中使用到了win32gui 或者 ctypes 请试验者在装完python,相应的安装 pywin32 或者 ctypes。
其中想请教高手,因为SystemParametersInfo功能函数的局限性,此程序只能更新bmp图片桌面,这让人很不爽,我想知道如何支持更新所有壁纸类型 即'bmp', 'jpg', 'gif', 'jpeg', 'dib', 'png',请赐教!!
贴出此程序希望和大家交流,也希望大家自己逐步完善此程序,谢谢!
感觉有具体目标作程序,要比单纯看语法学习有意思,并且更有效的多,不是吗?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061016/ed921970/attachment.htm
2006年10月25日 星期三 13:41
我昨天也刚好想换一下桌面背景。 只能是bmp的。 用了PIL(Python Imaging Library),其实就是把jpg、png做了转换成bmp的。 On 10/16/06, iconic <iconic at 126.com> wrote: > > 大家好: > 好久没有在此留言了,但一直关注着这里,看别人的交流受益匪浅。由于本人平时不使用python语言,所以自学速度很慢。 > 周末没有什么事情,所以写了一个小python例子,用于自动更改桌面 > 壁纸。(看到同事使用番茄花园xp中有此功能,所以就像用python实现一下),由于本人对python的理解还不够,所以下面的代码中一定有很多不如人意的地方,希望大家多给指正,谢谢。 > 注此程序只在xp下运行过,用python 2.4.3 开发,代码如下: > > import sys, os, time, win32gui# 也可以使用 ctypes 代替win32gui 同事恢复响应的注释语句即可 > > print '改变桌面程序,运行开始' > > finalfile = [] #保存最后要显示的图片集和 > file_type = ['bmp']#'jpg', 'gif', 'jpeg', 'dib', 'png' 设置文件类型 > length = 0 #循环长度 > pos = 0 #当前循环位置 > > if sys.platform[0:3] 'win': #系统判断 > sys.exit("此程序只适用于windows") > > dire = "C:/Documents and Settings/sinic/My Documents/My Pictures/zelda" > #图片目录设置,请自行修改 > if not os.path.exists(dire): #目录有效性判断 > print "Not find dirction, please affirm your input!" > sys.exit("文件选择有误,运行停止") > list = os.listdir(dire) #提取目录中所有列表 > for filename in list: #判断及组合有效文件 文件扩展名 > if len(filename.split('.')) == 2: #消除目录判断 > if filename.split('.') [1] in file_type: #扩展名判断 > finalfile.append(filename) > if finalfile.__eq__([]): #是否有有效文件 > sys.exit("没有图片文件,运行停止") > > SPI_SETDESKWALLPAPER = 0x14 #查msdn中解释 20 是指设置壁纸 > > length = len(finalfile) #壁纸循环显示 > while True: > if pos < length: > pass > #ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, > dire+'/'+finalfile[pos] , 0) > win32gui.SystemParametersInfo(SPI_SETDESKWALLPAPER, > dire+'/'+finalfile[pos] , 0) > pos = pos+1 > time.sleep(60) #60秒 = 1分钟 更换一次 > else: > pass > pos = 0 #回到显示图片集和中的第一张图片 > > 此代码有很多不足之处,功能部分它只是基本实现桌面替换功能。 其中使用到了win32gui 或者 ctypes 请试验者在装完python,相应的安装 > pywin32 或者 ctypes。 > > 其中想请教高手,因为SystemParametersInfo功能函数的局限性,此程序只能更新bmp图片桌面,这让人很不爽,我想知道如何支持更新所有壁纸类型 > 即'bmp', 'jpg', 'gif', 'jpeg', 'dib', 'png',请赐教!! > > 贴出此程序希望和大家交流,也希望大家自己逐步完善此程序,谢谢! > > 感觉有具体目标作程序,要比单纯看语法学习有意思,并且更有效的多,不是吗? > > > > > > > 美 女 看 了 就 想 要 (图) > 绝 对 另 类 ! 和 老 婆 长 假 期 间 的 败 家 记 录 > <http://adtaobao.allyes.com/main/adfclick?db=adtaobao&bid;=600,597,58&cid;=30015,198,1&sid;=32501&show;=ignore&url;=http://www.taobao.com/vertical/lady/pro.php> > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20061025/c0db47b9/attachment.htm
2006年10月25日 星期三 14:18
我原来写的一个换桌面的程序,里面的核心程序借用了那个blog里的代码。
也是只能用bmp的图片,原作者用了pygame的image做了转换。
这个程序里面比较大的篇幅用来记日志,其实核心内容只有一点点~
好像只能在XP下用,2000下会有莫名错误。
我用pyinstaller打的包,系统启动时自动运行,但偶尔也会跑不起来,不知道为啥。
import dynwin.windll as windll
import os, os.path
import random
import time
import pygame.image as image
class Wallpaper:
def __init__(self):
self.SOURCE = 'Pictures'
self.tempfile = 'TempWallpaper.bmp'
self.success = True
self.warning = ''
self.filename = ''
pass
def excute_change(self):
try:
logfile = open('LogFile.log', 'r')
count = logfile.readlines()
logfile.close()
except:
count = []
print len(count)
if len(count) >= 500:
logfile = open('LogFile.log', 'w')
logfile.write('Log the changing of Wallpaper')
logfile.close()
try:
self.wallpaper = self.get_rand_wallpaper()
if self.wallpaper[-3:] == ('jpg' or 'JPG'):
self.wallpaper = self.get_bmp_wallpaper(self.wallpaper)
self.set_wallpaper(self.wallpaper)
self.set_logfile()
except:
print "Failure to change the wallpaper"
#------------------------------------------------------------------------------------------------
# Notes of the following three functions: get_rand_wallpaper, set_wallpaper,
# and get_bmp_wallpaper
# The core mind of these three are from
# http://www.dormforce.net/Blog/xebec/archive/2006/05/30/9281.aspx
#------------------------------------------------------------------------------------------------
def get_bmp_wallpaper(self, wallpaper):
pic = image.load(wallpaper)
image.save(pic, self.tempfile)
return self.tempfile
def get_rand_wallpaper(self):
tempdir = os.listdir(self.SOURCE)
tempdir = [i for i in tempdir if os.path.splitext(i)[1] in ('.bmp',
'.jpg')]
print tempdir
self.filename = random.choice(tempdir)
return os.path.join(self.SOURCE, self.filename)
def set_wallpaper(self, wallpaper):
try:
x = windll.module('user32')
filename = windll.cstring(wallpaper)
self.success = x.SystemParametersInfo(20, 0, filename, 3)
except:
self.warning = 'Warning! There is something wrong!'
self.content += ('\n' + self.warning)
#------------------------------------------------------------------------------------------------
def set_logfile(self):
logfile = open('LogFile.log', 'a')
datetime = time.ctime()
self.content = '\n\n' + datetime
if self.success:
self.state = 'Changing Wallpaper is Successful!'
else:
self.state = 'Changing Wallpaper is Failure!'
if self.warning:
self.state = ''
self.content += ('\n' + self.filename + ' is choosed')
self.content += ('\n' + self.state)
self.content += ('\n' + 60 * '-')
logfile.write(self.content)
logfile.close()
if __name__ == '__main__':
wallpaper = Wallpaper()
while 1:
wallpaper.__init__()
wallpaper.excute_change()
t = random.randint(180, 600)
time.sleep(t)
在06-10-25,otherrrr <otherrrr at gmail.com> 写道:
>
> 我昨天也刚好想换一下桌面背景。
> 只能是bmp的。
>
> 用了PIL(Python Imaging Library),其实就是把jpg、png做了转换成bmp的。
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061025/e3d12fe4/attachment.html
Zeuux © 2025
京ICP备05028076号