[TimLinux] PyQt5 安裝部署


1. 依賴包

Click (7.0)
PyQt5 (5.11.2)
PyQt5-sip (4.19.12)
QScintilla (2.10.7)
pip (9.0.1)
pyqt5-tools (5.11.2.1.3rc8)  提供了QtDesigner.exe工具
python-dotenv (0.9.1)
setuptools (28.8.0)
sip (4.19.8)

這些依賴包都能夠通過pip3 install *.whl 的包格式來安裝。

1.1. 安裝命令

PyQt5(不包含有 QtDesigner)

PS C:\Users\admin\Desktop> pip3 install PyQt5 -i https://pypi.douban.com/simple
Looking in indexes: https://pypi.douban.com/simple
Collecting PyQt5
  Downloading https://pypi.doubanio.com/packages/5d/85/d174a50e0d6b60aa9113f6a32afb31f2
5345bec8584992af486235373252/PyQt5-5.11.2-5.11.1-cp35.cp36.cp37.cp38-none-win_amd64.whl (93.3MB) 100% |████████████████████████████████| 93.4MB 1.1MB/s Collecting PyQt5_sip<4.20,>=4.19.11 (from PyQt5) Downloading https://pypi.doubanio.com/packages/3f/4f/7b820770e6a8f8b83cacea561534e31c7
8a74eeac0fb2f7618c835fa74c6/PyQt5_sip-4.19.12-cp36-none-win_amd64.whl (51kB) 100% |████████████████████████████████| 61kB 1.6MB/s Installing collected packages: PyQt5-sip, PyQt5 Successfully installed PyQt5-5.11.2 PyQt5-sip-4.19.12 PS C:\Users\admin\Desktop>

PyQt5-tools (包含有 QtDesigner)

PS C:\Users\admin\Desktop> pip3 install PyQt5-tools -i https://pypi.douban.com/simple
Looking in indexes: https://pypi.douban.com/simple
Collecting PyQt5-tools
  Downloading https://pypi.doubanio.com/packages/0e/a1/b2bbbb9e0c0f374fb77c85b014fc39fdb
6e9e258c20906cc7ecb5f565e38/pyqt5_tools-5.9.0.1.2-cp36-none-win_amd64.whl (37.5MB) 100% |████████████████████████████████| 37.5MB 6.6MB/s Installing collected packages: PyQt5-tools Successfully installed PyQt5-tools-5.9.0.1.2 PS C:\Users\admin\Desktop>

 1.2. 文檔API

https://pyqt.readthedocs.io/en/latest/sip-classes.html

https://riverbankcomputing.com/news --> https://pyqt.readthedocs.io/en/latest/

 

2. PyCharm配置

請參考:https://www.cnblogs.com/BlueSkyyj/p/8398277.html

簡略步驟說明:

  • 添加 External Tools:QtDesigner
  • 添加 External Tools:PyUIC(QtDesigner生成的.ui文件自動轉換為.py文件)

2.1. External Tools: QtDesigner

Settings -> Tools -> External Tools 選中“+”,彈出的對話框:

Name: QtDesigner
Tool Settings:
    Program: C:\Python365\Lib\site-packages\pyqt5_tools\designer.exe
    Parameters: $FileDir$  (這是一個宏,可以通過點擊Insert macro來添加這個值,手動輸入也是一樣的意思)
    Working directory: $FileDir$

 

 

2.2. External Tools: PyUIC

settings -> Tools -> External Tools 選中“+”,彈出的對話框:

Name: PyUIC
Tool settings:
    Program: C:\Python365\python.exe
    Parameters: -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
    Working directory: $FileDir$ (可以通過 insert macro來設置,也可以手動輸入)

 

 

3. 示例

創建一個純python工程(Pure Python Project)QtTest。

3.1. 啟動QtDesigner.exe

通過右鍵 -> External Tools -> QtDesigner,將啟動QtDesigner.exe,創建一個MainWindow,然后將左側的一個 PushButton 拖入 創建的MainWindow,保存文件到工程目錄,取名 hello.ui

3.2. UI轉換為PY

選中 hello.ui,然后右鍵 -> External Tools -> PyUIC,將自動把 hello.ui 轉換為 hello.py 文件了。

3.3. 編寫 main.py

在工程目錄中創建 main.py 文件,寫入如下內容:

import sys
import hello  #生成的 hello.py
from PyQt5.QtWidgets import QApplication, QMainWindow


if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = QMainWindow()
    ui = hello.Ui_MainWindow()
    ui.setUi(w)
    w.show()

    sys.exit(app.exec_())

3.4. 運行

main.py 文件內,點擊鼠標右鍵,選擇:Run,將彈出一個 Window 窗口。

3.5. 不啟動后台

使用 pythonw.exe 運行 GUI 程序,將不顯示 cmd 窗口,只會顯示 GUI 窗口,這點非常的有用,但是這個解析器同時關閉了原來的:stdin, stdout, stderr。解決方案參考鏈接:https://stackoverflow.com/questions/9705982/pythonw-exe-or-python-exe

 3.6. 另外一個示例

# 來自 《PyQt5 快速開發與實踐》
import sys
from PyQt5.QtWidgets import QPushButton, QApplication, QWidget


class WinForm(QWidget):
    def __init__(self, parent=None):
        super(WinForm, self).__init__(parent)
        self.setGeometry(300, 300, 350, 350)
        self.setWindowTitle('點擊按鈕關閉窗口')
        quit = QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)
        quit.setStyleSheet("background-color: red")
        quit.clicked.connect(self.close)
    

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = WinForm()
    win.show()
    sys.exit(app.exec_())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM