赞
踩
Python标准库更多的适合处理后台任务,唯一的图形库tkinter使用起来很不方便,所以后来出现了针对Python图形界面开发的扩展库,例如PyQt。
在介绍PyQt之前,必须先简单介绍一下Qt。Qt是一个C++可视化开发平台,是一个跨平台的C++图形用户界面应用程序框架(C++ GUI),能够为应用程序开发者提供建立图形用户界面所需的功能。Qt是完全面向对象的、易扩展,可应用于组件编程,并可以用于嵌入式开发。它是目前流行的Linux桌面环境KDE 的基础,是Linux和嵌入式操作系统下的主流图形界面开发环境,其最大优势在于只需编写一次代码,就能编译部署在任何操作系统和硬件上。因为擅长图形界面开发,如今更扩展到移动及嵌入式设备开发。对于商业软件公司来说极具价值,可以广泛应用于物联网特别是智能汽车、智能制造业等的研发。
PyQt是一个创建Python GUI应用程序的工具包,是Qt和Python结合的一个产物,可以说是为了将Qt的功能用于Python开发的一个Qt的Python包装器。它是Python编程语言和Qt库的成功融合。
PyQt上手难度还是比较高,不想掉发的,可以看我专栏其它的界面开发教程:https://blog.csdn.net/m0_59023219/category_12553272.html
学完本文教程可接着学习更深层次教程:基于PyQt Python的深度学习图像处理界面开发(二)-CSDN博客
第一步、安装:
说来搞笑,安装这一步,可能就会出现问题,你能遇到的问题,基本百分之99别人也遇到过,如若安装不成功或者运行代码界面异常出不来,百度呗。
- pip install pyqt5
-
- pip install pyqt5-tools
第二、Hello World代码:
- # -*- coding: utf-8 -*-
-
- # Form implementation generated from reading ui file 'new.ui'
- #
- # Created by: PyQt5 UI code generator 5.15.2
- #
- # WARNING: Any manual changes made to this file will be lost when pyuic5 is
- # run again. Do not edit this file unless you know what you are doing.
- import sys
- import cv2
- import time
- from os import getcwd
- from PyQt5.QtCore import Qt
- from PyQt5 import QtCore, QtGui, QtWidgets
- from PyQt5.QtWidgets import QFileDialog
- import numpy as np
-
-
- class Ui_Form(object):
- def setupUi(self, Form):
- Form.setObjectName("Form")
- Form.resize(490, 616)
-
- self.retranslateUi(Form)
-
- def retranslateUi(self, Form):
- _translate = QtCore.QCoreApplication.translate
- Form.setWindowTitle(_translate("Form", "微信公众号:天竺街潜水的八角"))
-
-
-
- if __name__ == "__main__":
- app = QtWidgets.QApplication(sys.argv)
-
- baseWidget = QtWidgets.QWidget() # 创建窗口的基类QWidget的实例
-
- ui = Ui_Form() # 创建UI窗口的实例
- ui.setupUi(baseWidget) # 以baseWidget作为传递参数
-
- baseWidget.show()
- ##ui.LabHello.setText("Hello,被程序修改") #可以修改窗体上标签的文字
-
- sys.exit(app.exec_())
第三步、使用语法:
我这里总结一些常用的语法,比如按钮打开,文本显示结果,显示图片
用来给用户点击,来完成某种动作的控件,一般是矩形,一般作为登录按钮,注册按钮,关闭按钮
- # -*- coding: utf-8 -*-
-
- # Form implementation generated from reading ui file 'new.ui'
- #
- # Created by: PyQt5 UI code generator 5.15.2
- #
- # WARNING: Any manual changes made to this file will be lost when pyuic5 is
- # run again. Do not edit this file unless you know what you are doing.
- import sys
- import cv2
- import time
- from os import getcwd
- from PyQt5.QtCore import Qt
- from PyQt5 import QtCore, QtGui, QtWidgets
- from PyQt5.QtWidgets import QFileDialog
- import numpy as np
-
-
- class Ui_Form(object):
- def setupUi(self, Form):
- Form.setObjectName("Form")
- Form.resize(490, 616)
- self.label = QtWidgets.QLabel(Form)
- self.label.setGeometry(QtCore.QRect(50, 20, 381, 20))
- self.label.setObjectName("label")
-
- self.pushButton = QtWidgets.QPushButton(Form)
- self.pushButton.setGeometry(QtCore.QRect(50, 60, 93, 28))
- self.pushButton.setObjectName("pushButton")
-
- self.retranslateUi(Form)
-
- def retranslateUi(self, Form):
- _translate = QtCore.QCoreApplication.translate
- Form.setWindowTitle(_translate("Form", "微信公众号:天竺街潜水的八角"))
- self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">微信公众号:天竺街潜水的八角</span></p></body></html>"))
- self.pushButton.setText(_translate("Form", "选择"))
-
-
-
- if __name__ == "__main__":
- app = QtWidgets.QApplication(sys.argv)
-
- baseWidget = QtWidgets.QWidget() # 创建窗口的基类QWidget的实例
-
- ui = Ui_Form() # 创建UI窗口的实例
- ui.setupUi(baseWidget) # 以baseWidget作为传递参数
-
- baseWidget.show()
- ##ui.LabHello.setText("Hello,被程序修改") #可以修改窗体上标签的文字
-
- sys.exit(app.exec_())
QLabel控件是PyQt中一个常用的控件,它不仅可以作为一个占位符显示不可编辑的文本或图片(展示GIF动画图片),还可以被用作提示标记为其他控件;一些纯文本、链接或富文本可以显示在QLabel标签上
- # -*- coding: utf-8 -*-
-
- # Form implementation generated from reading ui file 'new.ui'
- #
- # Created by: PyQt5 UI code generator 5.15.2
- #
- # WARNING: Any manual changes made to this file will be lost when pyuic5 is
- # run again. Do not edit this file unless you know what you are doing.
- import sys
- import cv2
- import time
- from os import getcwd
- from PyQt5.QtCore import Qt
- from PyQt5 import QtCore, QtGui, QtWidgets
- from PyQt5.QtWidgets import QFileDialog
- import numpy as np
-
-
- class Ui_Form(object):
- def setupUi(self, Form):
- Form.setObjectName("Form")
- Form.resize(490, 616)
- self.label = QtWidgets.QLabel(Form)
- self.label.setGeometry(QtCore.QRect(50, 20, 381, 20))
- self.label.setObjectName("label")
-
- self.retranslateUi(Form)
-
- def retranslateUi(self, Form):
- _translate = QtCore.QCoreApplication.translate
- Form.setWindowTitle(_translate("Form", "微信公众号:天竺街潜水的八角"))
- self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">微信公众号:天竺街潜水的八角</span></p></body></html>"))
-
-
-
-
- if __name__ == "__main__":
- app = QtWidgets.QApplication(sys.argv)
-
- baseWidget = QtWidgets.QWidget() # 创建窗口的基类QWidget的实例
-
- ui = Ui_Form() # 创建UI窗口的实例
- ui.setupUi(baseWidget) # 以baseWidget作为传递参数
-
- baseWidget.show()
- ##ui.LabHello.setText("Hello,被程序修改") #可以修改窗体上标签的文字
-
- sys.exit(app.exec_())
QImage 类在pyqt中的作用主要是I/O和直接逐像素点访问图像数据,QImage类提供了一个硬件无关的图像表示方法,该图像可以逐像素被访问和用于画图设备;故可以通过该 QImage 类对工业相机获取到的图像数据进行显示
- # -*- coding: utf-8 -*-
-
- # Form implementation generated from reading ui file 'new.ui'
- #
- # Created by: PyQt5 UI code generator 5.15.2
- #
- # WARNING: Any manual changes made to this file will be lost when pyuic5 is
- # run again. Do not edit this file unless you know what you are doing.
- import sys
- import cv2
- import time
- from os import getcwd
- from PyQt5.QtCore import Qt
- from PyQt5 import QtCore, QtGui, QtWidgets
- from PyQt5.QtWidgets import QFileDialog
- import numpy as np
-
-
- class Ui_Form(object):
- def setupUi(self, Form):
- Form.setObjectName("Form")
- Form.resize(490, 616)
- self.label = QtWidgets.QLabel(Form)
- self.label.setGeometry(QtCore.QRect(50, 20, 381, 20))
- self.label.setObjectName("label")
-
- self.pushButton = QtWidgets.QPushButton(Form)
- self.pushButton.setGeometry(QtCore.QRect(50, 60, 93, 28))
- self.pushButton.setObjectName("pushButton")
-
- self.label_2 = QtWidgets.QLabel(Form)
- self.label_2.setGeometry(QtCore.QRect(50, 100, 400, 400))
- self.label_2.setObjectName("label_2")
-
- self.retranslateUi(Form)
-
- def retranslateUi(self, Form):
- _translate = QtCore.QCoreApplication.translate
- Form.setWindowTitle(_translate("Form", "微信公众号:天竺街潜水的八角"))
- self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">微信公众号:天竺街潜水的八角</span></p></body></html>"))
- self.pushButton.setText(_translate("Form", "选择"))
-
- frameClone = cv2.imread("background.png")
- frameClone = cv2.resize(frameClone,(400,400))
- # 在Qt界面中显示人脸
- show = cv2.cvtColor(frameClone, cv2.COLOR_BGR2RGB)
- showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
- self.label_2.setPixmap(QtGui.QPixmap.fromImage(showImage))
- QtWidgets.QApplication.processEvents()
-
-
-
- if __name__ == "__main__":
- app = QtWidgets.QApplication(sys.argv)
-
- baseWidget = QtWidgets.QWidget() # 创建窗口的基类QWidget的实例
-
- ui = Ui_Form() # 创建UI窗口的实例
- ui.setupUi(baseWidget) # 以baseWidget作为传递参数
-
- baseWidget.show()
- ##ui.LabHello.setText("Hello,被程序修改") #可以修改窗体上标签的文字
-
- sys.exit(app.exec_())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。