Python 3.2和Qt不兼容? [英] Incompatibility between Python 3.2 and Qt?

查看:125
本文介绍了Python 3.2和Qt不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Python 3.2和PyQt 4.8.6时遇到问题
似乎Python 3.2找不到导入。
特别是 Q方法。例如下面的QString。

 从PyQt4导入QtCore,QtGui 
试试:
_fromUtf8 = QtCore。 QString.fromUtf8
除了AttributeError:
_fromUtf8 = lambda s:s

Python 2.7一切正常。
代码中的错误在哪里?

解决方案

Python3进行了许多不兼容的更改,以便清理语言,并且在一定程度上,PyQt通过引入某些API的更多pythonic版本做到了这一点。但是,对于Python2 Python3,可以在逐个类的基础上选择这些不同的API版本,因此唯一真正的区别是为每个Python版本选择的默认值。 b $ b

在Python2中, QString 的默认API版本是 v1,将其实现为Python类型。在Python3中,默认值为 v2,它会自动与适当的Python字符串对象之间进行转换。



可以使用 sip 包中> setapi 函数。因此,要继续在应用程序中使用 QString 类,只需确保在首次导入PyQt模块之前设置了适当的版本即可。

  import sip 
sip.setapi('QString',1)

从PyQt4导入QtCore,QtGui
尝试:
_fromUtf8 = QtCore.QString.fromUtf8
除了AttributeError:
_fromUtf8 = lambda s:s

有关可以通过这种方式设置的所有API的详细信息,请参见此处


I have problems with Python 3.2 and PyQt 4.8.6 It seems as if Python 3.2 can`t find the imports. Especially the "Q"-methods. For example the QString below.

from PyQt4 import QtCore, QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

With Python 2.7 everything works fine. Where is the mistake in my code?

解决方案

Python3 made many incompatible changes in order to "clean up" the language, and, to a certain extent, PyQt has done the same by introducing "more pythonic" versions of some APIs. But these different API versions can be selected on a class by class basis for both Python2 and Python3, so the only real difference is the defaults chosen for each Python version.

In Python2, the default API version for QString is "v1", which implements it as a Python type; in Python3 the default is "v2", which automatically converts to and from the appropriate Python string object.

The API version can be selected by using the setapi function from the sip package. So to continue using the QString class in your application, just make sure the appropropriate version is set before the PyQt modules are first imported:

import sip
sip.setapi('QString', 1)

from PyQt4 import QtCore, QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

For details of all the APIs that can be set this way, see here.

这篇关于Python 3.2和Qt不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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