您现在的位置是:首页 >技术教程 >VSCode 配置PyQt5 开发环境网站首页技术教程
VSCode 配置PyQt5 开发环境
简介VSCode 配置PyQt5 开发环境
前提要求:Windows 安装Anaconda。
如果没有安装,请参考文章:Windows安装Anaconda使用教程
1、PyQt 5 安装
在配置python环境的时候需要注意,PyQt5需要python 3.5+以上的版本,在python环境中(conda、pipenv)安装pyqt5相关组件PyQt5以及PyQt5-tools(designer)
pip install pyqt5
pip install pyqt5-tools
# 查看虚拟环境
C:WindowsSystem32>conda env list
# conda environments:
#
base D:anaconda3
python310 D:anaconda3envspython310
# 选择虚拟环境
C:WindowsSystem32>activate python310
# conda 自带pyqt5
(python310) C:WindowsSystem32>pip install pyqt5
Requirement already satisfied: pyqt5 in d:anaconda3envspython310libsite-packages (5.15.7)
Requirement already satisfied: PyQt5-sip<13,>=12.11 in d:anaconda3envspython310libsite-packages (from pyqt5) (12.11.0)
# 安装pyqt5-tools 工具
(python310) C:WindowsSystem32>pip install pyqt5-tools
Collecting pyqt5-tools
Downloading pyqt5_tools-5.15.9.3.3-py3-none-any.whl (29 kB)
Collecting pyqt5==5.15.9
Downloading PyQt5-5.15.9-cp37-abi3-win_amd64.whl (6.8 MB)
---------------------------------------- 6.8/6.8 MB 1.4 MB/s eta 0:00:00
Collecting python-dotenv
Downloading python_dotenv-1.0.0-py3-none-any.whl (19 kB)
Collecting pyqt5-plugins<5.15.9.3,>=5.15.9.2.2
Downloading pyqt5_plugins-5.15.9.2.3-cp310-cp310-win_amd64.whl (66 kB)
---------------------------------------- 66.8/66.8 kB 3.5 MB/s eta 0:00:00
Collecting click
Downloading click-8.1.3-py3-none-any.whl (96 kB)
---------------------------------------- 96.6/96.6 kB ? eta 0:00:00
Collecting PyQt5-Qt5>=5.15.2
Downloading PyQt5_Qt5-5.15.2-py3-none-win_amd64.whl (50.1 MB)
---------------------------------------- 50.1/50.1 MB 5.0 MB/s eta 0:00:00
Requirement already satisfied: PyQt5-sip<13,>=12.11 in d:anaconda3envspython310libsite-packages (from pyqt5==5.15.9->pyqt5-tools) (12.11.0)
Collecting qt5-tools<5.15.2.2,>=5.15.2.1.2
Downloading qt5_tools-5.15.2.1.3-py3-none-any.whl (13 kB)
Collecting colorama
Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Collecting qt5-applications<5.15.2.3,>=5.15.2.2.2
Downloading qt5_applications-5.15.2.2.3-py3-none-win_amd64.whl (64.5 MB)
---------------------------------------- 64.5/64.5 MB 4.0 MB/s eta 0:00:00
Installing collected packages: PyQt5-Qt5, qt5-applications, python-dotenv, pyqt5, colorama, click, qt5-tools, pyqt5-plugins, pyqt5-tools
Attempting uninstall: pyqt5
Found existing installation: PyQt5 5.15.7
Uninstalling PyQt5-5.15.7:
Successfully uninstalled PyQt5-5.15.7
Successfully installed PyQt5-Qt5-5.15.2 click-8.1.3 colorama-0.4.6 pyqt5-5.15.9 pyqt5-plugins-5.15.9.2.3 pyqt5-tools-5.15.9.3.3 python-dotenv-1.0.0 qt5-applications-5.15.2.2.3 qt5-tools-5.15.2.1.3
2、 VsCode中安装和配置pyqt插件
1.下载并安装PYQT Integration 插件,搜索PYQT Integration关键字
2. 配置pyqt integration
在插件安装完成后需要进行配置,配置Pyuic和Qtdesigner的文件路径
Pyuic: 写入pyuic5.exe的完整路径(在python环境目录中搜索此文件,查看目录)
Qtdesigner: 写入designer.exe的完整路径(在python环境目录中搜索此文件,查看目录)
3 使用PYQT中的Qtdesigner和Pyuic工具
新建一个.ui文件,使用Qtdesigner工具设计界面
使用Pyuic工具,将讲设计好的ui转换成对应的python代码
生成了Ui_one.py文件,将.ui文件转换成.py文件。
4、添加PyQT5程序入口
添加Python 程序入口, main 方法。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'e:py_workspacepy-qt-demoone.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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.QtWidgets import QApplication,QMainWindow
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")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
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", "MainWindow"))
if __name__ == '__main__':
# 只有直接运行这个脚本,才会往下执行
# 别的脚本文件执行,不会调用这个条件句
# 实例化,传参
app = QApplication(sys.argv)
# 创建对象
mainWindow = QMainWindow()
# 创建ui,引用demo1文件中的Ui_MainWindow类
ui = Ui_MainWindow()
# 调用Ui_MainWindow类的setupUi,创建初始组件
ui.setupUi(mainWindow)
# 创建窗口
mainWindow.show()
# 进入程序的主循环,并通过exit函数确保主循环安全结束(该释放资源的一定要释放)
sys.exit(app.exec_())
执行效果
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。