Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序

pycharm版本为:

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第1张图片

 

  • 安装PyQt5-tools

在终端执行pip命令安装完PyQt5-tools。

pip install PyQt5-tools

 

  • 配置PyCharm

file---settings---External Tools---点➕,添加两项内容

1. Qt DesignerPycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第2张图片

Name

Qt Designer

Program

C:\Users\a\Anaconda3\Lib\site-packages\pyqt5_tools\Qt\bin\designer.exe

Working directory

$ProjectFileDir$

 

2. PyUIC

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第3张图片

Name

PyUIC

Program

C:\Users\a\Anaconda3\python.exe

Arguments

C:\Users\a\Anaconda3\Lib\site-packages\PyQt5\uic\pyuic.py $FileName$ -o $FileNameWithoutExtension$.py

Working directory

$ProjectFileDir$

 

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第4张图片

  • 使用工具

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第5张图片

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第6张图片

保存之后生成.ui的文件

右击.ui文件,运行PyUIC生成同名的.py文件

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第7张图片

 

在.py文件尾追加下面代码即可运行:

if __name__ == '__main__':

    import sys

    app = QtWidgets.QApplication(sys.argv)

    widget = QtWidgets.QMainWindow()

    ui = Ui_MainWindow() #这里改成你自己的项目名称

    ui.setupUi(widget)

    widget.show()

    sys.exit(app.exec_())

 

 

  • 报错解决

from .driver import Driver 报错

 

将文件C:\Users\a\Anaconda3\Lib\site-packages\PyQt5\uic\pyuic.py

修改为:

from PyQt5.uic.driver import Driver

from PyQt5.uic.exceptions import NoSuchClassError, NoSuchWidgetError

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第8张图片

 

  • 参考资料

pyqt5教程

http://code.py40.com/category/asc6

 

PyCharm+QTDesigner+PyUIC使用教程

https://blog.csdn.net/u013247461/article/details/85063284

 

  • pyinstaller在windows下的安装
pip install pyinstaller

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第9张图片

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第10张图片

 

  • pyinstaller打包为exe文件
pyinstaller --onefile --nowindowed main.py

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第11张图片

Pycharm安装PyQt5 & 使用pyinstaller将Python脚本导出为exe程序_第12张图片

基本语法:
pyinstaller options myscript.py
常用的可选参数如下:
--onefile 将结果打包成一个可执行文件
--onedir 将所有结果打包到一个文件夹中,该文件夹包括一个可执行文件和可执行文件执行时需要的依赖文件(默认)
--paths=DIR 设置导入路径
--distpath=DIR 设置将打包的结果文件放置的路径
--specpath=DIR 设置将spec文件放置的路径
--windowed 使用windows子系统执行,不会打开命令行(只对windows有效)
--nowindowed 使用控制台子系统执行(默认)(只对windows有效)
--icon= 将file.ico添加为可执行文件的资源(只对windows有效)

如pyinstaller --paths="D:\Queena" guess_exe.py

参考资料:https://www.cnblogs.com/robinunix/p/8426832.html#_label3

你可能感兴趣的:(python)