为什么这个 ctypes 代码不能与 Python 3.3 一起使用,但可以与 Python 2.7 一起使用?

编程入门 行业动态 更新时间:2024-10-22 09:39:48
本文介绍了为什么这个 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 编码的输入字符串作为参数,即 mbcsPython 中的编码.

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 一起使用?

本文发布于:2023-10-26 23:34:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能与   代码   ctypes   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!