使用Qt Designer开发上位机

一、安装第三方库

使用pip命令安装第三方库

bash 复制代码
pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyqt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple

二、使用designer设计界面

1、找到对应的designer.exe,双击进入界面设计。

2、创建一个主界面。

2.1 实现一个按钮弹窗的功能

1、拖出来一个按钮

2、关联UI背后的动作事件

3、保存.ui文件

bash 复制代码
pyuic5 -o main.py .\main.ui

4、把生成的.ui文件生成对应的py文件

5、手动实现按钮背后的逻辑

2.2 测试

测试ok

三、源码

python 复制代码
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '.\main.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.


from PyQt5 import QtCore, QtGui, QtWidgets

import sys
from PyQt5.QtWidgets import QApplication,QMainWindow


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1017, 984)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(450, 180, 271, 101))
        font = QtGui.QFont()
        font.setPointSize(19)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1017, 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)
        self.pushButton.clicked.connect(self.button_on) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "QT_demo"))
        self.pushButton.setText(_translate("MainWindow", "打开"))

    # 手动实现背后的逻辑
    def button_on(self):
        QtWidgets.QMessageBox.information(self.pushButton, "提示", "你点击了按钮")




if __name__ == "__main__":
    app = QApplication(sys.argv)

    mainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(mainWindow)
    mainWindow.show()
    sys.exit(app.exec_())
XML 复制代码
<?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>1017</width>
    <height>984</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>QT_demo</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>450</x>
      <y>180</y>
      <width>271</width>
      <height>101</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>19</pointsize>
     </font>
    </property>
    <property name="text">
     <string>打开</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1017</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>pushButton</sender>
   <signal>clicked()</signal>
   <receiver>MainWindow</receiver>
   <slot>button_on()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>540</x>
     <y>289</y>
    </hint>
    <hint type="destinationlabel">
     <x>550</x>
     <y>527</y>
    </hint>
   </hints>
  </connection>
 </connections>
 <slots>
  <slot>button_on()</slot>
 </slots>
</ui>
相关推荐
xuxigifxfh1 分钟前
HJ4 字符串分隔
java·开发语言·华为机考
databook11 分钟前
什么是范数?用 NumPy 动手算一遍就明白了
python·数学·numpy
MayZork19 分钟前
Qt 开发集成CMake + vcpkg
开发语言·c++·qt
朝朝辞暮i21 分钟前
VLA 系统学习第 5 课:神经网络的参数到底是什么?——从 nn.Linear 真正理解 W、 b 和 Gradient
人工智能·python·深度学习·神经网络·vla
XZ-07000124 分钟前
week7-文本
python
零基础12331 分钟前
PDF 处理工具全攻略:pdf24、PDFgear、Adobe Acrobat DC 与 Stirling-PDF 深度对比
java·开发语言·pdf
Java后端的Ai之路34 分钟前
01-React基础教程
开发语言·前端·python·react.js·前端框架
书生执笔画浮沉43 分钟前
Linux桌面端应用向鸿蒙PC迁移适配纪实
c++·qt·c·信创·海思·libc·ohos
我的xiaodoujiao1 小时前
Django 基础知识详细图文教程 14-Django 表单定义与使用
开发语言·后端·python·django
(Charon)1 小时前
【C++面试】单例模式:懒汉式与饿汉式的实现、区别与线程安全
开发语言·c++·面试