这里我在Win7 x64上运行Python 2.7.3(x64),PyQt 4.9.5-1(x64)。我想将一个简单的PyQt脚本转换为exe文件 这是我的python脚本:
#!/usr/bin/env python
import sys
from PyQt4 import Qt
a = Qt.QApplication(sys.argv)
def sayHello():
print "Hello, World!"
hellobutton = Qt.QPushButton("Say 'Hello world!'",None)
a.connect(hellobutton, Qt.SIGNAL("clicked()"), sayHello)
hellobutton.show()
a.exec_()
从命令行运行它按预期工作。 我使用setup.py for py2exe:
from distutils.core import setup
import py2exe
setup(console=['pyqt-example.py'])
但是,如果我尝试将其转换为带有python setup.py py2exe
的py2exe 0.6.9的exe文件,运行exe文件时会出现此错误:
Traceback (most recent call last):
File "pyqt-example.py", line 6, in <module>
a = Qt.QApplication(sys.argv)
AttributeError: 'module' object has no attribute 'QApplication'
我还尝试了\Python27\Scripts\cxfreeze pyqt-example.py --target-dir dist
的cx_freeze 4.3。这导致:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec code in m.__dict__
File "pyqt-example.py", line 6, in <module>
a = Qt.QApplication(sys.argv)
AttributeError: 'module' object has no attribute 'QApplication'
所以我假设我错过了通知这些工具有关某些Qt组件的位置。究竟我错过了什么?
答案 0 :(得分:0)
感谢Avaris,你的提示是正确的。避免使用模块Qt就是答案。这是可以正常工作的脚本:
#!/usr/bin/env python
import sys
from PyQt4 import QtGui,QtCore
a = QtGui.QApplication(sys.argv)
def sayHello():
print "Hello, World!"
hellobutton = QtGui.QPushButton("Say 'Hello world!'",None)
a.connect(hellobutton, QtCore.SIGNAL("clicked()"), sayHello)
hellobutton.show()
a.exec_()
之后,我必须调用cx_freeze:cxfreeze pyqt-example.py --include-modules atexit --target-dir dist
。它有效!