下载与安装Pycharm专业版
下载与安装Python3.6.5
VS2015专业版(可选)
安装OpenCV与PyQt5
安装OpenCV4.4
代码语言:javascript复制pip install opencv-python
安装PyQt5
代码语言:javascript复制pip install pyqt5
pip install PyQt5-tools
配置Pycharm开发环境
打开Pycharm,新建一个项目
选择到你安装的python3.6.5对应的解释器,这步很关键!然后填好项目名称,然后点击【create】按钮即可。
然后从【File】->【Settings】中选择Extenal Tools,如下图所示:
点击添加,QT Designer,PyUIC两个工具,它们的设置如下图:
代码语言:javascript复制C:UsersAdministratorAppDataLocalProgramsPythonPython36Libsite-packagesqt5_applicationsQtbindesigner.exe
-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
设置好之后,点击确认,从最上部【Tools】->【Extenal Tools】中选择QT Designer 打开如下所示:
PyQy5快速开发测试
就可以直接通过pycharm调用QT Designer来设计界面了!我设计的界面如下:
然后保存文件为test.ui
右键test.ui 选择转换为python代码,得到的代码如下:
代码语言:javascript复制# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(80, 110, 591, 371))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(80, 20, 91, 41))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
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", "OpenCV学堂-QT OpenCV Python版本"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "OpenCV学堂"))
添加一段调用代码,
代码语言:javascript复制if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
image = QtGui.QPixmap('D:/images/lena.jpg')
ui.label.setPixmap(image)
ui.label.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.show()
sys.exit(app.exec_())
运行代码,测试结果如下:
从此再也不用担心我用Python不会写UI了
踩坑指南:
配置与加载QT Designer问题
VCRUNTIME140_1.dll无法找到问题!
搜索,下载这个文件,然后扔到windows/system32下面就好啦,亲测有效。
2. 无法加载Platform问题,导致QT Desinger启动失败
需要把
代码语言:javascript复制C:UsersAdministratorAppDataLocalProgramsPythonPython36Libsite-packagesqt5_applicationsQtplugins
目录下的platform整个文件夹copy到
代码语言:javascript复制C:UsersAdministratorAppDataLocalProgramsPythonPython36Libsite-packagesqt5_applicationsQtbin
目录下,然后再启动就好啦
君子藏器于身,待时而动