1. 當前使用的 python版本 :python
2. 使用pip安裝 QT5:git
pip install pyqt5app
3. 安裝 PyQt5-tools : pip install pyqt5-tools函數
4. 校驗QT是否安裝成功:工具
from PyQt5 import QtWidgets, QtGui import sys app = QtWidgets.QApplication(sys.argv) window = QtWidgets.QWidget(); window.show() sys.exit(app.exec_())
彈出以下界面便可:性能
Pycharm相關配置:ui
一、添加external Toolsthis
打開settings->Tools->External Tools點擊「+」spa
二、添加QtDesigner 3d
以下圖設置
三、添加PyUIC
以下圖設置。
Parameters: -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
三、新建一個PyQt demo,建立一個項目
以下圖
以下圖打開QtDesigner
打開Qt界面以下圖
如圖選擇建立一個Main Window
如圖能夠根據須要添加本身的組件
以下圖保存
以下圖,使用PyUIC將hello.ui文件轉換爲hello.py
下面是我作的一個簡單工具 :
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'query.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1119, 616) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout.setObjectName("gridLayout") self.text_netype = QtWidgets.QLineEdit(self.centralwidget) self.text_netype.setObjectName("text_netype") self.gridLayout.addWidget(self.text_netype, 0, 1, 1, 1) self.label_indicator_id = QtWidgets.QLabel(self.centralwidget) font = QtGui.QFont() font.setPointSize(13) self.label_indicator_id.setFont(font) self.label_indicator_id.setLayoutDirection(QtCore.Qt.RightToLeft) self.label_indicator_id.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter) self.label_indicator_id.setObjectName("label_indicator_id") self.gridLayout.addWidget(self.label_indicator_id, 0, 2, 1, 1) self.text_indicator_id = QtWidgets.QLineEdit(self.centralwidget) self.text_indicator_id.setInputMethodHints(QtCore.Qt.ImhDigitsOnly) self.text_indicator_id.setObjectName("text_indicator_id") self.gridLayout.addWidget(self.text_indicator_id, 0, 3, 1, 1) self.pushButton = QtWidgets.QPushButton(self.centralwidget) font = QtGui.QFont() font.setPointSize(13) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.gridLayout.addWidget(self.pushButton, 0, 4, 1, 1) self.tableWidget = QtWidgets.QTableWidget(self.centralwidget) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(9) self.tableWidget.setRowCount(0) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(3, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(4, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(5, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(6, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(7, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(8, item) self.gridLayout.addWidget(self.tableWidget, 1, 0, 1, 5) self.label_netype = QtWidgets.QLabel(self.centralwidget) font = QtGui.QFont() font.setPointSize(13) self.label_netype.setFont(font) self.label_netype.setLayoutDirection(QtCore.Qt.RightToLeft) self.label_netype.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter) self.label_netype.setObjectName("label_netype") self.gridLayout.addWidget(self.label_netype, 0, 0, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1119, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "查詢性能指標公式")) self.label_indicator_id.setText(_translate("MainWindow", "指標ID")) self.pushButton.setText(_translate("MainWindow", "查詢")) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "DEV_TYPE_NAME")) item = self.tableWidget.horizontalHeaderItem(1) item.setText(_translate("MainWindow", "DEV_TYPE_ID")) item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("MainWindow", "DEV_TYPE_VERSION")) item = self.tableWidget.horizontalHeaderItem(3) item.setText(_translate("MainWindow", "INDICATOR_GRP_ID")) item = self.tableWidget.horizontalHeaderItem(4) item.setText(_translate("MainWindow", "OS_VERSION")) item = self.tableWidget.horizontalHeaderItem(5) item.setText(_translate("MainWindow", "PRIORITY")) item = self.tableWidget.horizontalHeaderItem(6) item.setText(_translate("MainWindow", "PROTOCOL")) item = self.tableWidget.horizontalHeaderItem(7) item.setText(_translate("MainWindow", "INDICATOR_ID")) item = self.tableWidget.horizontalHeaderItem(8) item.setText(_translate("MainWindow", "FORMULA")) self.label_netype.setText(_translate("MainWindow", "設備類型"))
這個QT自動生成的py文件,看到只是對控件的位置屬性進行定義而已 ,界面須要有類合併來支撐這個界面。用過Qt的應該知道。界面有了還須要一個類與界面合併,能夠想像這個類至關於cpp和.h文件的關係
咱們來定義一個類
from query import Ui_MainWindow from PyQt5 import QtCore, QtGui, QtWidgets import sys from mylog import LogAssistant class query_window(QtWidgets.QMainWindow): def __init__(self): QtWidgets.QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.pushButton.clicked.connect(self.query_formula) # 給button 的 點擊動做綁定一個事件處理函數 def query_formula(self): # 此處編寫具體的業務邏輯
最後添加程序運行的入口代碼:
if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = query_window() window.show() sys.exit(app.exec_())