vscode搭建PyQt + Quick开发环境

icon

VScode搭建PyQt + Quick开发环境



环境准备 🔔

  • 🔥 安装Python
  • 🔥 安装Visual Studio Code

安装必要的Python包 🔔

PyQt有两种库PyQt5PySide2

🔎 PyQt5和PySide2的区别

PyQt5PySide2都提供了Python对Qt库的绑定,选择PyQt5还是PySide2,主要取决于你的项目需求和许可要求。如果需要在闭源项目中使用,且不想购买商业许可证,PySide2可能是更好的选择。如果你希望获得更丰富的社区资源和教程,PyQt5则是不错的选择。

  1. 授权和许可 📄

    • PyQt5:采用GPL(GNU General Public License)或商业许可证。如果你的项目是闭源的,你需要购买商业许可证。
    • PySide2:由Qt公司(现已被The Qt Company维护)开发,采用LGPL(Lesser General Public License)或商业许可证。LGPL允许在闭源项目中使用,前提是遵守动态链接和库的修改发布要求。

  2. 开发者和支持 📄

    • PyQt5:由Riverbank Computing开发和维护,有长期的社区支持。
    • PySide2:由Qt公司开发和维护,随着Qt框架的更新得到官方支持。

  3. 命名空间和模块 📄

    • PyQt5:在导入模块时使用from PyQt5 import …
    • PySide2:在导入模块时使用from PySide2 import …,尽管接口和PyQt5非常相似,但有时需要注意命名空间的差异。

💾 安装PyQt5

需要使用pip安装PyQt5PyQt5-tool两个包

  • 📄 安装PyQt5

    pip install pyqt5
    

    如果下载异常或者下载过久,可以使用镜像安装:

    pip install -i https://mirrors.aliyun.com/pypi/simple/ PyQt5
    
  • 📄 安装PyQt5-tool

    pip install pyqt5-tool
    

    如果下载异常或者下载过久,可以使用镜像安装:

    pip install -i https://mirrors.aliyun.com/pypi/simple/ PyQt5-tool
    

💾 安装PySide2

pip install PySide2

如果下载异常或者下载过久,可以使用镜像安装:

pip install -i https://mirrors.aliyun.com/pypi/simple/ PySide2


配置VScode 🔔

💻 安装扩展

📄扩展 📜作用
在vscode中使用Python开发必须下载的扩展:
  • 语法高亮
  • 代码补全
  • 格式化
  • 代码导航
  • 其他
允许你使用VSCode的调试功能来调试Python应用程序。
使用QWidget库进行开发的可安装,可以调用QtDesigner拖拽控件

代码示例 🔔

✔ Python调用Qt库的示例

main.py中编写python示例:

# main.py
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, QTimer, QDateTime
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType

class Backend(QObject):
    mySignal = pyqtSignal(str)
    
    def __init__(self, parent=None):
        QObject.__init__(self)
        self.timer = QTimer(self)
        self.timer.timeout.connect(lambda: self.mySignal.emit(QDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss")))
        self.timer.start(1000)

    @pyqtSlot(str)
    def onButtonClicked(self, text):
        print(text)

if __name__ == "__main__":
    app = QGuiApplication([])
    engine = QQmlApplicationEngine()
    qmlRegisterType(Backend, "Backend", 1, 0, "Backend")
    engine.load("main.qml")
    app.exec_()

✔ 编写Quick 界面

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2
import Backend 1.0

Window {
    title: qsTr("Hulu PyQml Example")
    visible: true

    width: 300
    height: 100
    property Backend backend: Backend {}

    ColumnLayout {
        spacing: 10
        anchors.fill: parent        
        
        Text {
            Layout.alignment: Qt.AlignHCenter
            Layout.preferredWidth: 100
            Layout.preferredHeight: 30
            id: text
        }
        Button {
            id: button
            Layout.preferredWidth: 100
            Layout.preferredHeight: 30
            Layout.alignment: Qt.AlignHCenter
            text: "Click me"
            onClicked: {
                backend.onButtonClicked(button.text)
            }
        }
    }
    Connections {
        target: backend
        function onMySignal(str) {
            text.text = str
        }
    }
}

✔ 程序界面

在这里插入图片描述

示例工程源码下载https://download.csdn.net/download/qq_41898196/89557468


发布 🔔

关于编译好程序之后进行打包发布,可以参考该文章《通过PyInstaller把Python文件打包成应用程序》


支持 🔔

如果你觉得这个文章有帮助,请给它一个 👍 和 ⭐!

相关推荐

  1. 使用VSCodeVue 3开发环境

    2024-07-20 06:42:01       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 06:42:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 06:42:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 06:42:01       45 阅读
  4. Python语言-面向对象

    2024-07-20 06:42:01       55 阅读

热门阅读

  1. Azure MySQL资源优化策略

    2024-07-20 06:42:01       15 阅读
  2. IP地址与mac地址的绑定、解绑

    2024-07-20 06:42:01       20 阅读
  3. Go的数据结构与实现【LinkedList】

    2024-07-20 06:42:01       19 阅读
  4. postgres 的WAL日志膨胀的几种原因

    2024-07-20 06:42:01       19 阅读
  5. 桥接模式(Bridge Pattern)

    2024-07-20 06:42:01       19 阅读
  6. Metalog 源码解读

    2024-07-20 06:42:01       13 阅读
  7. 452. 用最少数量的箭引爆气球

    2024-07-20 06:42:01       15 阅读
  8. Python爬虫——1爬虫基础(一步一步慢慢来)

    2024-07-20 06:42:01       11 阅读
  9. 2024.7.19 Ai大模型问答 - 底特律和长春

    2024-07-20 06:42:01       17 阅读
  10. 【python】python面向对象之——继承

    2024-07-20 06:42:01       18 阅读