首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ctype在python中创建后台转换器,不起作用

使用ctype在python中创建后台转换器,不起作用
EN

Stack Overflow用户
提问于 2014-02-12 08:04:23
回答 3查看 3.7K关注 0票数 1

我正在编写一个简单的(我认为是)程序,为一周中的每一天设置不同的桌面背景。它运行时没有任何错误,但什么也没有发生。图像的路径有效。有什么想法吗?

代码语言:javascript
运行
复制
import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20

localtime = time.localtime(time.time())
wkd = localtime[6]

if wkd == 6:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\1.jpg",0)

elif wkd == 0:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\2.jpg",0)

elif wkd == 1:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\3.jpg",0)

elif wkd == 2:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\4.jpg",0)

elif wkd == 3:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\5.jpg",0)

elif wkd == 4:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\6.jpg",0)

elif wkd == 5:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\7.jpg",0)
EN

回答 3

Stack Overflow用户

发布于 2015-05-04 03:45:38

我一定已经阅读了关于这个主题的所有现有站点,在放弃之前,我读到了这个工作代码(win7 pro 64位,Python3.4)

代码语言:javascript
运行
复制
import ctypes
SPI_SETDESKWALLPAPER = 0x14     #which command (20)

SPIF_UPDATEINIFILE   = 0x2 #forces instant update
src = r"D:\Downloads\_wallpapers\3D-graphics_Line_025147_.jpg" #full file location
#in python 3.4 you have to add 'r' before "path\img.jpg"

print(ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, src, SPIF_UPDATEINIFILE))

它使用SystemParametersInfoW代替SystemParametersInfoA (W代替A)。

希望它能帮助你和许多其他有类似问题的人。

票数 4
EN

Stack Overflow用户

发布于 2016-12-31 15:10:58

如果你使用的是Python3,你应该使用ctypes.windll.user32.SystemParametersInfoW而不是ctypes.windll.user32.SystemParametersInfoA( this answer说的是W而不是A)。Another answer对此进行了描述,因为在Python3中,str类型的形式是UTF-16,就像C中的wchar_t *一样。

更重要的是,请最小化代码,如下所示:

代码语言:javascript
运行
复制
import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20

wallpapers = r"C:\Users\Owner\Documents\Wallpaper\%d.jpg"

localtime = time.localtime(time.time())
wkd = localtime[6]
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, wallpapers%(wkd+1), 0)

不要重复你的话。

票数 2
EN

Stack Overflow用户

发布于 2014-02-12 08:21:41

这不是对你的问题的回答,但是你通常可以通过这样做来缩小你的程序并删除冗余:

代码语言:javascript
运行
复制
import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20

wallpapers = [
    r"C:\Users\Owner\Documents\Wallpaper\1.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\2.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\3.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\4.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\5.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\6.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\7.jpg",
]

localtime = time.localtime(time.time())
wkd = localtime[6]
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, wallpapers[wkd], 0)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21715895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档