AttributeError: '模块'对象没有属性'QtString
我的开发环境:
操作系统:Windows XP
Python版本:python-3.1.2.msi
PyQt版本:PyQt-Py3.1-gpl-4.7.4-1.exe
代码:
import sys
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
s = QtCore.QtString()
sys.exit(app.exec_())
它总是给我显示:
在 'module' 中
s = QtCore.QtString()
AttributeError: 'module' 对象没有属性 'QtString'
我修改了代码:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
app = QApplication(sys.argv)
s = QtString()
sys.exit(app.exec_())
然后它总是给我显示这样的内容:
在 'module' 中
s = QtString()
NameError: 名称 'QtString' 未定义
我该怎么办?
2 个回答
2
你是想说 QString
而不是 QtString
吗?
(你可以在 Python 解释器里输入 help(QtCore)
,然后搜索 string
)
6
这个问题在这里有解释 http://inputvalidation.blogspot.com/2010/10/python3-pyqt4-and-missing-qstring.html
你无法加载 QString
的原因是因为它在 PyQt4 中缺失(可能在更早的版本中也是这样,谁知道呢)。自从 Python 3 开始,和 Python 2 不同,默认支持 Unicode,所以这个类就不再需要了。
为了兼容性,你应该在你的 import
语句附近使用下面的代码片段:
try:
from PyQt4.QtCore import QString
except ImportError:
QString = str