Python 使用QT5开发界面的一个demo开发过程的总结


1. 当前使用的 python版本 :

 

2. 使用pip安装 QT5:

pip install pyqt5

 

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相关配置:

1、添加external Tools

打开settings->Tools->External Tools点击“+”

 

 

2、添加QtDesigner 

如下图设置

 

3、添加PyUIC

 如下图设置。

 Parameters:  -m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py

 

 

3、新建一个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_())

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号