PyQt5编写简单界面

来源:互联网 发布:软件或者神器 知乎 编辑:程序博客网 时间:2024/06/07 08:50

PyQt5编写简单界面


环境配置

Requirements:PythonPyQt5(Python和PyQt5可以通过anaconda傻瓜式安装)QtCreator

QtCreator界面设计

step1: 新建Qt界面设计器项目

这里写图片描述

step2: 设计界面

这里写图片描述

step3: 将ui文件转换成肮脏的py文件
pyuic5 mainwindow.ui > mainwindow_ui.py转换后的文件其实就是用python代码写的界面,如果会直接写的话可以不使用界面设计器。# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'mainwindow.ui'## Created by: PyQt5 UI code generator 5.6## WARNING! All changes made in this file will be lost!import face_recognitionfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtWidgets import QFileDialogclass Ui_MainWindow(object):    def setupUi(self, MainWindow):        MainWindow.setObjectName("MainWindow")        MainWindow.resize(783, 729)        self.centralwidget = QtWidgets.QWidget(MainWindow)        self.centralwidget.setObjectName("centralwidget")        self.widget = QtWidgets.QWidget(self.centralwidget)        self.widget.setGeometry(QtCore.QRect(20, 20, 751, 411))        self.widget.setObjectName("widget")        ...        self.retranslateUi(MainWindow)        QtCore.QMetaObject.connectSlotsByName(MainWindow)        self.pushButton.clicked.connect(self.openFileNamesDialog)    def retranslateUi(self, MainWindow):        _translate = QtCore.QCoreApplication.translate        MainWindow.setWindowTitle(_translate("MainWindow", "Face Recognition Demo"))        self.groupBox.setTitle(_translate("MainWindow", "Input Image"))        ...

将signal和slot连起来

为了不破坏设计器生成的文件,另写一个ui的包装类,在该类里面定义回调函数并与ui成员变量事件相绑定,如:ui = Ui_MainWindow()def __init__(self, parent=None):    QtWidgets.QWidget.__init__(self, parent)    self.timerForShowCam.timeout.connect(self.draw)    self.timerForDetect.timeout.connect(self.drawDetectResult)    self.ui.label_2.clicked.connect(self.ui.slotLabel2Clicked)  

重写控件事件,自定义消息

比如我的项目里面想要完成label的clicked消息回调,但是标准label没有clicked消息,这个时候就要涉及控件事件的重写了,与C++类似,给出实现示例:继承QtWidgets.QLabel并重写其鼠标点击事件,发出clicked消息。class ClickedLabel(QtWidgets.QLabel):    clicked = QtCore.pyqtSignal()    def mousePressEvent(self, QMouseEvent):        super().mousePressEvent(QMouseEvent)        self.clicked.emit()

改变QGroupBox的样式

在Qt设计器中或者之间在python代码中插入代码段:

self.groupBox.setStyleSheet("QGroupBox{\n"                                    "border-width:2px;\n"                                    "border-style:solid;\n"                                    "border-radius: 10px;\n"                                    "border-color:gray;\n"                                    "margin-top:0.5ex;\n"                                    "}\n"                                    "QGroupBox::title{\n"                                    "subcontrol-origin:margin;\n"                                    "subcontrol-position:top left;\n"                                    "left:10px;\n"                                    "margin-left:0px;\n"                                    "padding:0 1px;\n"                                    "}")

主函数

if __name__ == '__main__':    app = QtWidgets.QApplication(sys.argv)    faceApp = faceRecGUI()    faceApp.show()    sys.exit(app.exec_())