PyQt5 基本语法(九):QtDesigner

QtDesigner

一、 简介

1、 搭建GUI方式

1.1 纯手码

一行一行的通过手写代码来实现上述效果

特点:

  • 工作量大
  • 新手会把代码结构搞乱

1.2 设计工具

通过可视化的设计工具,来按照所见所得的方式进行设计界面,然后自动转换成代码或者直接生成应用程序

特点:

  • 直观、高效
  • 通过可视化完成
  • 工作量小,方便修改调试
  • 界面和逻辑分离

正规开发中,使用此方式

2、 QtDesigner介绍

QtDesigner中的操作方式十分灵活,其通过拖拽的方式放置控件可以随时查看控件效果,并可预览效果

QtDesigner的设计符合MVC架构,实现了视图和逻辑分离,从而实现了开发的便捷

QtDesigner生成的.ui 文件(实质上是XML格式的文件)

  • 可以直接使用

    from PyQt5.uic import loadUi
    loadUi("文件名.ui")
  • 也可以通过pyui5工具转换成.py 文件

3、环境配置

安装:

pip install PyQt5-tools

ui转py:

pyuic5 $FileName$ -o ui_$FileNameWithoutExtension$.py -x
如:
pyuic5 test.ui -o ui_test.py -x

后面加的-x:其含义是转换为可执行函数

qrc 资源转换:

pyrcc5 $FileName$ -o $FileNameWithoutExtension$_rc.py
如:
pyrcc5 test.qrc -o test_rc.py

二、 使用示例

1、 生成ui文件

我设计的ui,并且转换后的文件代码为:

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'first.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(120, 50, 51, 21))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(180, 50, 113, 21))
self.lineEdit.setInputMask("")
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(150, 150, 93, 28))
self.pushButton.setStyleSheet("background-color: rgb(255, 170, 0)")
self.pushButton.setObjectName("pushButton")
self.label.setBuddy(self.lineEdit)
self.retranslateUi(Form)
self.pushButton.clicked.connect(Form.check_login)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "账号:"))
self.pushButton.setText(_translate("Form", "登陆"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())

2、 调用文件

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QApplication
from ui_first import Ui_Form
class Window(QWidget, Ui_Form): # 使用多继承的方法
def __init__(self):
super().__init__()
self.setupUi(self) # 调用父类的方法
def check_login(self): # 为继承的ui文件中的按钮添加点击事件
print("按钮被点击,输入的内容为", self.lineEdit.text())
if __name__ == '__main__':
# 可以通过导包来运行窗口
import sys
app = QApplication(sys.argv)
# 创建窗口
w = Window()
# 显示窗口
w.show()
sys.exit(app.exec_())
posted @   Kenny_LZK  阅读(303)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示