Why won#39;t this ctypes code work with Python 3.3 but will work with Python 2.7?(为什么这个 ctypes 代码不能与 Python 3.3 一起使用,但可以与 Python 2.7 一起使用?)
问题描述
所以我正在尝试制作一个 Python 3.3 程序来使用 ctypes 模块更改 Windows 桌面背景.我已经在 Python 2.7 中测试了以下代码,并且运行良好.但它不适用于 Python 3.3!我使用的是 Windows 7.这是代码:
So I'm trying to make a Python 3.3 program to change the Windows desktop background using the ctypes module. I've tested the following code in Python 2.7, and it worked perfectly. But it just won't work with Python 3.3! I'm using Windows 7. Here's the code:
import ctypes
SPI_SETDESKTOPWALLPAPER=20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKTOPWALLPAPER, 0,"C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg", 3)
推荐答案
SystemParametersInfoA
需要一个 8 位 ANSI 编码的输入字符串作为参数,即 mbcs
Python 中的编码.
SystemParametersInfoA
requires a 8-bit ANSI encoded input string as a parameter, which is known as mbcs
encoding in Python.
您将不得不在 python3 中使用 SystemParametersInfoW
.这是因为 SystemParametersInfoW
接受一个 UTF-16 宽的字符串(在 C 中是 wchar_t *
)并且 ctypes
库会自动转换这个传递的 unicodec_wchar_p
中的参数.
You will have to use SystemParametersInfoW
in python3. This is because SystemParametersInfoW
takes in a UTF-16 wide string (which is wchar_t *
in C) and the ctypes
library automatically converts this passed unicode argument into c_wchar_p
.
请参阅 文档更多细节.
这篇关于为什么这个 ctypes 代码不能与 Python 3.3 一起使用,但可以与 Python 2.7 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么这个 ctypes 代码不能与 Python 3.3 一起使用,但可以与 Python 2.7 一起使用?


- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01