美文网首页码农的世界
Python3脚本在Windows10下生成单个EXE文件!

Python3脚本在Windows10下生成单个EXE文件!

作者: b4a0155c6514 | 来源:发表于2019-01-12 11:46 被阅读0次
    Python3脚本在Windows10下生成单个EXE文件!

    本文环境配置:

    系统=>windows10:64位

    语言=>Python:3.7.1

    第三方库****pywin32:224****PyInstaller:3.4****PyQT5:5.11.3

    Python3脚本在Windows10下生成单个EXE文件!

    工具=>PyCharm:2018.3.1

    1、安装Python

    官网下载对应系统位数的版本:

    使用pip命令安装第三方库:

    Python3脚本在Windows10下生成单个EXE文件!

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install pywin32
    pip install PyInstaller
    pip install PyQT5
    </pre>

    2、安装PyCharm

    首先编写需要转换exe的Python代码,使用PyQT5做一个简单的GUI

    创建一个窗体

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import sys
    from PyQt5.Qt import *
    app = QApplication(sys.argv)

    实例对象

    窗体大小

    window = QWidget()
    window.resize(500, 500)
    window.setWindowTitle('测试')
    window.move(400, 200)

    显示窗体

    window.show()
    sys.exit(app.exec_())
    </pre>

    在窗体上添加一个标签

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 标签
    label = QLabel(window)
    label.setText('测试显示')
    label.move(250, 100)
    </pre>

    定义两个槽函数,功能是修改标签上的内容

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 槽函数1
    def btn_Old():
    label.setText('测试显示')

    槽函数2

    def btn_New():
    label.setText('点我干啥')
    </pre>

    在窗体上添加两个按钮,并链接槽函数

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 按钮1
    btn1 = QPushButton(window)
    btn1.setText('恢复显示')
    btn1.move(200, 200)
    btn1.clicked.connect(btn_Old)

    信号与槽

    按钮2

    btn1 = QPushButton(window)
    btn1.setText('修改显示')
    btn1.move(300, 200)
    btn1.clicked.connect(btn_New)

    信号与槽

    </pre>

    一个简单的GUI搞定,完整代码如下,代码并未做优化,只是测试用。文件名称定义为test.py

    Python3脚本在Windows10下生成单个EXE文件!

    转换EXE

    打开命令窗口,切换到test.py文件的路径下

    Python3脚本在Windows10下生成单个EXE文件!

    输入命令

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pyinstaller -F -w test.py
    </pre>

    Python3脚本在Windows10下生成单个EXE文件!

    开始执行代码,运行完成之后,在text.py文件的目录下有一个dist文件夹, 里面就有生成的EXE文件

    Python3脚本在Windows10下生成单个EXE文件!

    搞定,直接双击运行编写好的GUI程序。

    PyInstaller部分参数的含义

    -F 表示生成单个可执行文件

    -w 表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!

    -i 表示可执行文件的图标

    PyInstaller注意点

    1、有一些代码需要调用一些图片和资源文件的,这是不会自动导入的,需要你自己手动复制进去才行。不然exe文件运行时命令窗口会报错找不到这个文件。

    2、当你使用错误的参数去打包或者打包到一半中断,等等此类运行到一半没了的情况。会导致你原来的py文件变成一个0KB的空文件。里面的代码会全部消失!!!所以以后需要有个良好的习惯,就是复制一份代码出来,用这个副本进行打包。并且参数出错,或者打错了导致失败时,检查下副本文件的py文件是否还存在再继续重新打包,不然打出来的就是空的文件,自然一直闪退,因为压根没内容。

    3、写代码的时候应当有个良好的习惯,用什么函数导什么函数,不要上来import整个库,最后你会发现你一个100KB的代码打包出来有500MB,全是库,简直无语

    从今往后,专心Python

    相关文章

      网友评论

        本文标题:Python3脚本在Windows10下生成单个EXE文件!

        本文链接:https://www.haomeiwen.com/subject/lhnqdqtx.html