天天看點

Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式

pycharm版本為:

Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式
  • 安裝PyQt5-tools

在終端執行pip指令安裝完PyQt5-tools。

pip install PyQt5-tools
           
  • 配置PyCharm

file---settings---External Tools---點➕,添加兩項内容

1. Qt Designer

Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式
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程式
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程式
  • 使用工具
Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式
Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式

儲存之後生成.ui的檔案

右擊.ui檔案,運作PyUIC生成同名的.py檔案

Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式

在.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程式
  • 參考資料

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程式
Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式
  • pyinstaller打包為exe檔案
pyinstaller --onefile --nowindowed main.py
           
Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式
Pycharm安裝PyQt5 & 使用pyinstaller将Python腳本導出為exe程式

基本文法:

pyinstaller options myscript.py

常用的可選參數如下:

--onefile 将結果打包成一個可執行檔案

--onedir 将所有結果打包到一個檔案夾中,該檔案夾包括一個可執行檔案和可執行檔案執行時需要的依賴檔案(預設)

--paths=DIR 設定導入路徑

--distpath=DIR 設定将打包的結果檔案放置的路徑

--specpath=DIR 設定将spec檔案放置的路徑

--windowed 使用windows子系統執行,不會打開指令行(隻對windows有效)

--nowindowed 使用控制台子系統執行(預設)(隻對windows有效)

--icon=<FILE.ICO> 将file.ico添加為可執行檔案的資源(隻對windows有效)

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

參考資料:https://www.cnblogs.com/robinunix/p/8426832.html#_label3