为什么不能这样ctypes的使用Python 3.3 code的工作,但将与Python 2.7的工作? [英] Why won't this ctypes code work with Python 3.3 but will work with Python 2.7?

查看:422
本文介绍了为什么不能这样ctypes的使用Python 3.3 code的工作,但将与Python 2.7的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图使一个Python 3.3程序更改使用ctypes的模块的Windows桌面背景。我测试了以下code在Python 2.7,它完美地工作。但它只是不会与Python 3.3的工作!我使用的是Windows 7的下面是code:

 进口ctypes的
SPI_SETDESKTOPWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKTOPWALLPAPER,0,C:/用户/公共/图片/样品图片/ Penguins.jpg,3)


解决方案

SystemParametersInfoA 需要一个8位的ANSI连接codeD输入字符串作为参数,这是被称为 MBCS 编码在 Python的

您将在python3使用 SystemParametersInfoW 。这是因为 SystemParametersInfoW 接受一个UTF-16宽字符串(即 wchar_t的* C语言)和 ctypes的库自动转换这项通过UNI code参数为 c_wchar_p

请参考<一个href=\"https://docs.python.org/3.4/library/ctypes.html#accessing-functions-from-loaded-dlls\">documentation了解更多详情。

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 requires a 8-bit ANSI encoded input string as a parameter, which is known as mbcs encoding in Python.

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.

Refer the documentation for more details.

这篇关于为什么不能这样ctypes的使用Python 3.3 code的工作,但将与Python 2.7的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆