• mac下,vs code的PyQt6(PySide6)配置


    mac下,vs code的PyQt6、PySide6配置(使用anaconda)

    安装

    1. 推荐安装pyside6,和pyqt6一致,主要区别在开源协议。
    pip install pyside6
    
    • 1
    1. vs code 安装 Qt for python 插件。

    在这里插入图片描述

    配置

    要配置的参数一共有五个,包括

    1. Designer path
    2. rcc path
    3. uic path
    4. rcc args (复制即可)
    5. uic args (复制即可)

    在扩展页面点击 扩展设置 。进入扩展设置

    在这里插入图片描述

    点击 在setting.json中编辑

    在这里插入图片描述
    把下面的粘贴到末尾。然后开始替换前面三个的路径(下面为null的部分)

        "qtForPython.designer.path": null,
        "qtForPython.rcc.path": null,
        "qtForPython.uic.path": null,
       
        "qtForPython.uic.args": [
            "-o ${fileDirname}${pathSeparator}Ui_${fileBasenameNoExtension}.py"
        ],
        "qtForPython.rcc.args": [
            "-o ${fileDirname}${pathSeparator}rc_${fileBasenameNoExtension}.py"
        ],
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 其中rcc 文件 和uic 文件是在一起的。其路径为:“/Users/yong/opt/anaconda3/envs/pyqt/bin/pyside6-rcc” 在这里插入图片描述
    2. designer path 有点麻烦。用python pip show pyside6 查看pyside6的安装路径
      在这里插入图片描述
      打开Location,找到 PySide6 ,其中有 Designer.app ,右键 显示包内容 。然后 Contents -> MacOS ,这里有个Designer,即上面所需要的designer path。
      我的路径为 /Users/yong/opt/anaconda3/envs/pyqt/lib/python3.8/site-packages/PySide6/Designer.app/Contents/MacOS/Designer ,填入setting.json即可。

    这里Designer.app可以直接打开,完成设计后将ui文件编译为py文件,然后使用python导入,效果一致

    测试

    在vs code左侧资源管理器,右键点击最下面的 New Form
    在这里插入图片描述
    出现这些窗口表示Designer 配置成功

    在这里插入图片描述

    绘制一个窗口。然后对这个ui文件点击 Compile Form into Qt for Python File

    在这里插入图片描述

    出现了Ui_test.py表示编译成功。

    安装结束

    这里给一份我的代码以供大家测试

    附代码

    main.py

    import sys
    from PySide6 import QtCore, QtWidgets, QtGui
    import Ui_test 
    
    class TestWindow(QtWidgets.QMainWindow):
        def __init__(self):
            super().__init__()
            ui =  Ui_test.Ui_MainWindow()
            ui.setupUi(self)
        
        @QtCore.Slot()#槽函数用它装饰
        def login(self): #在Qt Designer中为登录按钮命名的槽函数;
            print("你点击了登录")
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        widget = TestWindow()
        widget.show()
        sys.exit(app.exec())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    test.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QLabel" name="label">
        <property name="geometry">
         <rect>
          <x>240</x>
          <y>180</y>
          <width>58</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>用户名:</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="textEdit">
        <property name="geometry">
         <rect>
          <x>290</x>
          <y>180</y>
          <width>211</width>
          <height>21</height>
         </rect>
        </property>
       </widget>
       <widget class="QLabel" name="label_2">
        <property name="geometry">
         <rect>
          <x>240</x>
          <y>220</y>
          <width>31</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>密码:</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="textEdit_2">
        <property name="geometry">
         <rect>
          <x>293</x>
          <y>220</y>
          <width>211</width>
          <height>21</height>
         </rect>
        </property>
       </widget>
       <widget class="QPushButton" name="pushButton">
        <property name="geometry">
         <rect>
          <x>330</x>
          <y>270</y>
          <width>100</width>
          <height>32</height>
         </rect>
        </property>
        <property name="text">
         <string>login</string>
        </property>
       </widget>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections>
      <connection>
       <sender>pushButton</sender>
       <signal>clicked()</signal>
       <receiver>MainWindow</receiver>
       <slot>login()</slot>
       <hints>
        <hint type="sourcelabel">
         <x>409</x>
         <y>283</y>
        </hint>
        <hint type="destinationlabel">
         <x>515</x>
         <y>340</y>
        </hint>
       </hints>
      </connection>
     </connections>
     <slots>
      <slot>login()</slot>
     </slots>
    </ui>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    Ui_test.py

    # -*- coding: utf-8 -*-
    
    ################################################################################
    ## Form generated from reading UI file 'test.ui'
    ##
    ## Created by: Qt User Interface Compiler version 6.3.1
    ##
    ## WARNING! All changes made in this file will be lost when recompiling UI file!
    ################################################################################
    
    from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
        QMetaObject, QObject, QPoint, QRect,
        QSize, QTime, QUrl, Qt)
    from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
        QFont, QFontDatabase, QGradient, QIcon,
        QImage, QKeySequence, QLinearGradient, QPainter,
        QPalette, QPixmap, QRadialGradient, QTransform)
    from PySide6.QtWidgets import (QApplication, QLabel, QMainWindow, QPushButton,
        QSizePolicy, QStatusBar, QTextEdit, QWidget)
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            if not MainWindow.objectName():
                MainWindow.setObjectName(u"MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QWidget(MainWindow)
            self.centralwidget.setObjectName(u"centralwidget")
            self.label = QLabel(self.centralwidget)
            self.label.setObjectName(u"label")
            self.label.setGeometry(QRect(240, 180, 58, 16))
            self.textEdit = QTextEdit(self.centralwidget)
            self.textEdit.setObjectName(u"textEdit")
            self.textEdit.setGeometry(QRect(290, 180, 211, 21))
            self.label_2 = QLabel(self.centralwidget)
            self.label_2.setObjectName(u"label_2")
            self.label_2.setGeometry(QRect(240, 220, 31, 16))
            self.textEdit_2 = QTextEdit(self.centralwidget)
            self.textEdit_2.setObjectName(u"textEdit_2")
            self.textEdit_2.setGeometry(QRect(293, 220, 211, 21))
            self.pushButton = QPushButton(self.centralwidget)
            self.pushButton.setObjectName(u"pushButton")
            self.pushButton.setGeometry(QRect(330, 270, 100, 32))
            MainWindow.setCentralWidget(self.centralwidget)
            self.statusbar = QStatusBar(MainWindow)
            self.statusbar.setObjectName(u"statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            self.pushButton.clicked.connect(MainWindow.login)
    
            QMetaObject.connectSlotsByName(MainWindow)
        # setupUi
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
            self.label.setText(QCoreApplication.translate("MainWindow", u"\u7528\u6237\u540d:", None))
            self.label_2.setText(QCoreApplication.translate("MainWindow", u"\u5bc6\u7801\uff1a", None))
            self.pushButton.setText(QCoreApplication.translate("MainWindow", u"login", None))
        # retranslateUi
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    今天小编给你介绍前端监控 SDK 开发分享
    架构核心技术之分布式消息队列
    源码解析springbatch的job是如何运行的?
    多测师肖sir_高级金牌讲师__接口测试之F12查看接口(2)
    MATLAB中plot3函数用法
    数据结构(C语言)——单链表
    聚观早报|高德发布安全出行大模型;小鹏G9焕新上市
    Java-设计Bird/Fish类
    并发、并行、同步、异步、阻塞、非阻塞
    python学习-----logging模块
  • 原文地址:https://blog.csdn.net/qq_41726670/article/details/125503192