使用cx_Freeze打包PyQt4程序

 PyQt源程序exam1.py:
  1. #!/usr/bin/env python
  2. import sys,sip    !!sip是Qt要求的,必须加
  3. from PyQt4 import QtGui,QtCore
  4. class MyWidget(QtGui.QWidget):
  5.     def __init__(self, parent=None):
  6.         QtGui.QWidget.__init__(self, parent)
  7.         self.setFixedSize(200120)
  8.         self.quit = QtGui.QPushButton("Quit"self)
  9.         self.quit.setGeometry(62407530)
  10.         self.quit.setFont(QtGui.QFont("Times"18, QtGui.QFont.Bold))
  11.         self.connect(self.quit, QtCore.SIGNAL("clicked()"),
  12.                      QtGui.qApp, QtCore.SLOT("quit()"))
  13. app = QtGui.QApplication(sys.argv)
  14. widget = MyWidget()
  15. widget.show()
  16. sys.exit(app.exec_())
打包命令:
D:/liusp/study/pyQT/exams>d:/cx_Freeze-3.0.3/FreezePython.exe
 --shared-lib-name=d:/Python25/lib/site-packages/PyQt4/QtCore4.dll
 --shared-lib-name=d:/Python25/lib/site-packages/PyQt4/QtGui4.dll
 --shared-lib-name=d:/python25/lib/site-packages/PyQt4/mingwm10.dll
 --install-dir dist exam1.py
这三个dll都是PyQt运行时需用到的,最后一行指定打包输出目录。
打包完成后得到dist目录,其包含如下文件:
exam1.exe mingwm10.dll PyQt.QtCore.pyd PyQt4.QtGui.pyd python25.dll QtCore4.dll QtCore4.dll sip.pyd

使用cx_freeze可以将PyQt5程序打包为可执行文件,使其在没有Python环境的机器上也能运行。 首先,确保已经安装了cx_freeze库。可以通过pip命令进行安装,如:pip install cx_Freeze。 然后在PyQt5的程序文件所在目录下,创建一个名为setup.py的脚本文件。 在setup.py文件中,需要导入cx_Freeze的一些模块,如: ```python from cx_Freeze import setup, Executable ``` 接下来,需要定义一些程序的信息,例如程序的名称、版本号、作者等等。同时,还需要指定要打包PyQt5程序的入口文件,如: ```python exe = Executable( script="main.py", base="Win32GUI", # 使用Windows的图形界面 icon="icon.ico" # 可选,设置程序的图标 ) ``` 再然后,可以定义一些打包的选项,例如需要排除的模块、包含的文件等等。例如: ```python options = { "build_exe": { "packages": ["pyqt5", "sys"], "excludes": ["tkinter"], "include_files": ["resources/"] # 将资源文件夹一同打包 } } ``` 最后,在setup函数中,指定一些打包的参数,例如程序的名称、版本号、作者等等。同时,还需要指定要打包的文件及其依赖项,如: ```python setup( name="MyApp", version="1.0", author="My Name", description="My PyQt5 app", executables=[exe], options=options ) ``` 保存并关闭setup.py文件。 在命令行中,切换到setup.py所在的目录,并执行以下命令: ```python python setup.py build ``` 执行完毕后,在build目录中会生成一个可执行文件,就是我们打包好的PyQt5程序。 需要注意的是,由于打包时会将PyQt5库及其依赖项一同打包,所以打包后的文件可能会比较大。如有需要,可以使用upx等压缩工具进行进一步压缩。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值