【计算机视觉】OpenCV篇(4)-Pycharm+PyQt5+Python⼩项⽬实战
1.下载安装
(1)Pycharm:下载链接
(2)推荐使⽤Qt Designer来设计界⾯,如果你装的是Anaconda的话,就已经⾃带了,我这⾥使⽤的是Pycharm的虚拟环境,通过以下的⽅式安装:(venv) G:\GitWorkspace\PycharmProjects\PythonStudy>pip install pyqt5
Collecting pyqt5
Downloading /packages/b2/87/219cd547c0deb902edd3fdbf1af01409f7f9d251344b9154d878203cf841/PyQt5-5.12.1-5.12.2-cp35.cp36.cp37.cp38-none-win_amd64.whl (47.4MB)    100% |████████████████████████████████| 47.4MB 40kB/s
Collecting PyQt5_sip<4.20,>=4.19.14 (from pyqt5)
Downloading /packages/ee/03/a11da2a5dd75b0ccd795ddbd944c18ce66
3ffb08d59a1e7f1ee5a597488c/PyQt5_sip-4.19.15-cp36-none-win_amd64.whl (51kB)
100% |████████████████████████████████| 61kB 35kB/s
Installing collected packages: PyQt5-sip, pyqt5
Successfully installed PyQt5-sip-4.19.15 pyqt5-5.12.1
(venv) G:\GitWorkspace\PycharmProjects\PythonStudy>pip install pyqt5-tools
Collecting pyqt5-tools
Downloading /packages/21/ff/7578d6d124926ff6cd1ef13c8c095063ad4c3811f4506788a915607aee81/pyqt5_tools-5.11.3.1.4-cp37-none-win32.whl (47.9MB)
100% |████████████████████████████████| 47.9MB 275kB/s
Collecting click (from pyqt5-tools)
Downloading /packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5
a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
100% |████████████████████████████████| 81kB 836kB/s
Collecting pyqt5==5.11.3 (from pyqt5-tools)
Downloading /packages/26/78/c215008e70982944272b6c329a76cc11259e7378a3e23418fbe0bd48de96/PyQt5-5.11.3-5.11.2-cp35.cp36.cp37.cp38-none-win32.whl (39.4MB)    100% |████████████████████████████████| 39.4MB 206kB/s
Collecting python-dotenv (from pyqt5-tools)
Downloading /packages/8c/14/501508b016e7b1ad0eb91bba581e66ad9bfc7c66fcacbb580eaf9bc38458/python_dotenv-0.10.1-py2.py3-none-any.whl
Requirement already satisfied: PyQt5_sip<4.20,>=4.19.11 in c:\mzhu_working1023\python-workspace\study\venv\lib\site-packages (from pyqt5==5.11.3->pyqt5-tools) (4.19.15)
Installing collected packages: click, pyqt5, python-dotenv, pyqt5-tools
Found existing installation: PyQt5 5.12.1
Uninstalling PyQt5-5.12.1:
Successfully uninstalled PyQt5-5.12.1
Successfully installed click-7.0 pyqt5-5.11.3 pyqt5-tools-5.11.3.1.4 python-dotenv-0.10.1 
2.环境配置
在Pycharm中,点击File->Settings->Tools->External Tools点击+添加以下⼯具:
(1)qtdesinger
(2)pyuic5,这个是把qt的UI⽂件转换成.py⽂件的⼯具
(3)pyrcc, 这个是将资源⽂件如图⽚等转成python代码能识别的⽂件,这个参数基本和pyuic5的是⼀样的
这样就基本完成PYQT5在Pycharm环境中的配置:
3.测试
(这⾥拿⼀个OpenCV的⼩练习来测试⼀下------)
打开qtdesinger,新建⼀个窗体:
界⾯的左侧是Qt的常⽤控件”Widget Box”,右侧有⼀个控件属性窗⼝”Property Editor”,其余暂时⽤不到。本例中我们只⽤到了”Push Button”控件和”Label”控件:最上⾯的三个Label控件⽤于显⽰图⽚,可以在属性窗⼝调整它的⼤⼩,我们统⼀调整到150×150:
另外,控件上显⽰的⽂字”text”属性和控件的名字”objectName”属性需要修改,便于显⽰和代码调⽤:
点击Designer⼯具栏的”Edit Signals/Slots”按钮,进⼊槽函数编辑界⾯,点击旁边的”Edit Widgets”可以恢复正常视图:
然后点击按钮并拖动,当产⽣类似于电路中的接地符号时释放⿏标,在弹出的配置窗⼝中,可以看到左侧是按钮的常⽤事件,我们选择点击事件”clicked()”,然后添加⼀个名为”btnOpenCamera_Clicked()”的槽函数:
重复上⾯的步骤,给五个按钮添加五个槽函数,最终结果如下:
到此,我们就完成了界⾯设计的所有⼯作,按下Ctrl+S保存当前窗⼝为.ui⽂件。.ui⽂件其实是按照XML格式标记的内容,可以⽤⽂本编辑器将.ui⽂件打开看看。
UI⽂件转py代码
因为我们是⽤Designer⼯具设计出的界⾯,并不是⽤Python代码敲出来的,所以要想真正运⾏,需要使⽤pyuic5将ui⽂件转成py⽂件。默认在%\Scripts\下,⽐如我的是在:G:\GitWorkspace\PycharmProjects\PythonStudy\venv\Scripts\
打开cmd命令⾏,切换到ui⽂件的保存⽬录。Windows下有个⼩技巧,可以在⽬录的地址栏输⼊cmd,⼀步切换到当前⽬录. 然后执⾏这条指令:
pyuic5 -o mainForm.py pyqt_create_ui.ui 
执⾏正常的话,就会⽣成mainForm.py⽂件,⾥⾯应该包含⼀个名为”Ui_MainWindow”的类。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'pyqt_create_ui.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")
self.btnOpenCamera = QtWidgets.alwidget)
self.btnOpenCamera.setGeometry(QtCore.QRect(60, 370, 100, 30))
self.btnOpenCamera.setObjectName("btnOpenCamera")
self.btnCaptureImg = QtWidgets.alwidget)
self.btnCaptureImg.setGeometry(QtCore.QRect(220, 370, 100, 30))
self.btnCaptureImg.setObjectName("btnCaptureImg")
self.btnOpenImg = QtWidgets.alwidget)
self.btnOpenImg.setGeometry(QtCore.QRect(370, 370, 93, 28))
self.btnOpenImg.setObjectName("btnOpenImg")
self.btnGraying = QtWidgets.alwidget)
self.btnGraying.setGeometry(QtCore.QRect(520, 370, 100, 30))
self.btnGraying.setObjectName("btnGraying")
self.btnThresholdSegment = QtWidgets.alwidget)
self.btnThresholdSegment.setGeometry(QtCore.QRect(670, 370, 150, 30))
self.btnThresholdSegment.setObjectName("btnThresholdSegment")
self.labelCamera = QtWidgets.alwidget)
self.labelCamera.setGeometry(QtCore.QRect(70, 100, 150, 150))
self.labelCamera.setObjectName("labelCamera")
self.labelCapture = QtWidgets.alwidget)
self.labelCapture.setGeometry(QtCore.QRect(330, 100, 150, 150))
self.labelCapture.setObjectName("labelCapture")
self.labelResult = QtWidgets.alwidget)
self.labelResult.setGeometry(QtCore.QRect(630, 100, 150, 150))
self.labelResult.setObjectName("labelResult")
MainWindow.alwidget)
MainWindow.ubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.t(MainWindow.btnOpenCamera_Clicked)
self.t(MainWindow.btnCaptureImg_Clicked)
self.t(MainWindow.btnOpenImg_Clicked)
self.t(MainWindow.btnGrayingImg_Clicked)
self.t(MainWindow.btnThresholdSegment_Clicked)
tSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = anslate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
python新建项目教程self.btnOpenCamera.setText(_translate("MainWindow", "Open Camera"))
self.btnCaptureImg.setText(_translate("MainWindow", "Capture Image"))
self.btnOpenImg.setText(_translate("MainWindow", "Open Image"))
self.btnGraying.setText(_translate("MainWindow", "Graying"))
self.btnThresholdSegment.setText(_translate("MainWindow", "Threshold Segmentation"))
self.labelCamera.setText(_translate("MainWindow", "Camera"))
self.labelCapture.setText(_translate("MainWindow", "Captured Image"))
self.labelResult.setText(_translate("MainWindow", "Result Image")) 
[注:mainForm.py⽂件是根据ui⽂件⽣成的,也就是说重新⽣成会覆盖掉。所以为了使界⾯与逻辑分离,我们需要新建⼀个逻辑⽂件。]
在同⼀⼯作⽬录下新建⼀个”mainEntry.py”的⽂件,存放逻辑代码。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。