VSCode + PYQT5 搭建图形化界面

时间:2023-12-29 21:15:38

1,安装依赖

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

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

2,找到designer 绘制ui界面程序

例:windows

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Lib\site-packages\pyqt5_tools\Qt\bin\designer.exe

绘制如下:

VSCode + PYQT5 搭建图形化界面

完事后保存 haha.ui

3,VSCode转化.ui文件为.py文件(安装PYQT Integration扩展)

右击haha.ui --- PYQT:Compile Form 会生成 Ui_haha.py 文件

然后调用UI类并运行py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(190, 200, 311, 81))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
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"))
self.pushButton.setText(_translate("MainWindow", "你好")) if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

结果如下:

VSCode + PYQT5 搭建图形化界面