使用 Visual Studio Code (VS Code) 开发 Python 图形界面程序

安装Python、VS Code

Documentation for Visual Studio Code

Python Releases for Windows | Python.org

更新pip

>python.exe -m pip install –upgrade pip
Requirement already satisfied: pip in c:\users\xxx\appdata\local\programs\python\python312\lib\site-packages (23.2.1)
Collecting pip
  Obtaining dependency information for pip from https://files.pythonhosted.org/packages/c9/bc/b7db44f5f39f9d0494071bddae6880eb645970366d0a200022a1a93d57f5/pip-25.0.1-py3-none-any.whl.metadata
  Downloading pip-25.0.1-py3-none-any.whl.metadata (3.7 kB)
Downloading pip-25.0.1-py3-none-any.whl (1.8 MB)
   —————————————- 1.8/1.8 MB 776.0 kB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 23.2.1
    Uninstalling pip-23.2.1:
      Successfully uninstalled pip-23.2.1
Successfully installed pip-25.0.1

安装PyQt5

>pip install PyQt5
Collecting PyQt5
  Downloading PyQt5-5.15.11-cp38-abi3-win_amd64.whl.metadata (2.1 kB)
Collecting PyQt5-sip<13,>=12.15 (from PyQt5)
  Downloading PyQt5_sip-12.17.0-cp312-cp312-win_amd64.whl.metadata (492 bytes)
Collecting PyQt5-Qt5<5.16.0,>=5.15.2 (from PyQt5)
  Downloading PyQt5_Qt5-5.15.2-py3-none-win_amd64.whl.metadata (552 bytes)
Downloading PyQt5-5.15.11-cp38-abi3-win_amd64.whl (6.9 MB)
   —————————————- 6.9/6.9 MB 884.3 kB/s eta 0:00:00
Downloading PyQt5_Qt5-5.15.2-py3-none-win_amd64.whl (50.1 MB)
   —————————————- 50.1/50.1 MB 1.1 MB/s eta 0:00:00
Downloading PyQt5_sip-12.17.0-cp312-cp312-win_amd64.whl (58 kB)
Installing collected packages: PyQt5-Qt5, PyQt5-sip, PyQt5
Successfully installed PyQt5-5.15.11 PyQt5-Qt5-5.15.2 PyQt5-sip-12.17.0

编码

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QMessageBox # type: ignore

# 创建主窗口
class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt 示例")

        # 添加标签
        label = QLabel("你好,PyQt!", self)

        # 添加按钮
        button = QPushButton("点击我", self)
        button.clicked.connect(self.on_button_click)

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(button)
        self.setLayout(layout)

    def on_button_click(self):
        QMessageBox.information(self, "提示", "你点击了按钮!")

# 运行应用程序
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

运行程序

程序示例

特此感谢DeepSeek提供的帮助。

作者:漫步企鹅

物联沃分享整理
物联沃-IOTWORD物联网 » 使用 Visual Studio Code (VS Code) 开发 Python 图形界面程序

发表回复