VSCode configures PyQt5 development environment

Prerequisite: Install Anaconda on Windows.

If it is not installed, please refer to the article: Windows installation Anaconda tutorial

1. PyQt 5 installation 

When configuring the python environment, you need to pay attention. PyQt5 requires python 3.5+ or above. Install pyqt5 related components PyQt5 and PyQt5-tools (designer) in the python environment (conda, pipenv)

pip install pyqt5

pip install pyqt5-tools
# 查看虚拟环境
C:\Windows\System32>conda env list
# conda environments:
#
base                     D:\anaconda3
python310                D:\anaconda3\envs\python310

# 选择虚拟环境
C:\Windows\System32>activate python310

# conda 自带pyqt5
(python310) C:\Windows\System32>pip install pyqt5
Requirement already satisfied: pyqt5 in d:\anaconda3\envs\python310\lib\site-packages (5.15.7)
Requirement already satisfied: PyQt5-sip<13,>=12.11 in d:\anaconda3\envs\python310\lib\site-packages (from pyqt5) (12.11.0)

# 安装pyqt5-tools 工具
(python310) C:\Windows\System32>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:\anaconda3\envs\python310\lib\site-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. Install and configure the pyqt plugin in VsCode

1. Download and install the PYQT Integration plugin, search for PYQT Integration keywords

2. Configure pyqt integration

After the plug-in is installed, it needs to be configured to configure the file path of Pyuic and Qtdesigner

 Pyuic: Write the full path of pyuic5.exe (search for this file in the python environment directory, view the directory)

 Qtdesigner: Write the full path to designer.exe (search for this file in the python environment directory, view the directory)

 

3 Using the Qtdesigner and Pyuic tools in PYQT

Create a new .ui file and use the Qtdesigner tool to design the interface

 Use the Pyuic tool to convert the designed ui into the corresponding python code

 The Ui_one.py file is generated, and the .ui file is converted into a .py file.

 4. Add PyQT5 program entry

Add Python program entry, main method.

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'e:\py_workspace\py-qt-demo\one.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_())

Execution effect

 

Guess you like

Origin blog.csdn.net/zhouzhiwengang/article/details/130194608