PyQt5 布局管理(水平、垂直、网格、表单、嵌套、QSplitter)

2023-11-09 16:40

本文主要是介绍PyQt5 布局管理(水平、垂直、网格、表单、嵌套、QSplitter),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 1. 布局管理
    • 2. 使用绝对位置布局
    • 3. QBoxLayout
        • addStretch() 添加可伸缩控件
    • 4. QGridLayout
    • 5. QFormLayout
    • 6. 嵌套布局
    • 7. QSplitter 布局

learn from 《PyQt5 快速开发与实战》
https://doc.qt.io/qtforpython/index.html
https://www.riverbankcomputing.com/static/Docs/PyQt5

1. 布局管理

  • QHBoxLayout 水平
  • QVBoxLayout 垂直
  • QGridLayout 网格
  • QFormLayout 表单布局,两列的形式
addLayout() 插入子布局
addWidget() 在布局中插入控件

2. 使用绝对位置布局

  • 使用 (x, y) 坐标
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplicationclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):lbl1 = QLabel('欢迎', self)# lbl1.move(15, 10)lbl2 = QLabel('学习', self)lbl2.move(35, 40)lbl3 = QLabel('PyQt5 !', self)lbl3.move(55, 70)self.setGeometry(300, 300, 320, 120)self.setWindowTitle('绝对位置布局例子')if __name__ == '__main__':app = QApplication(sys.argv)demo = Example()demo.show()sys.exit(app.exec_())

在这里插入图片描述

  • 缺点: 窗口大小变动时,控件大小和位置不会随动

3. QBoxLayout

  • stretch 参数设置伸缩量
# _*_ coding: utf-8 _*_
# @Time : 2022/6/4 18:49
# @Author : Michael
# @File : hbox.py
# @desc :from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QPushButton, QApplicationclass Winform(QWidget):def __init__(self):super(Winform, self).__init__()self.setWindowTitle('HBox')layout = QHBoxLayout()layout.addWidget(QPushButton('1'), 2, Qt.AlignTop)layout.addWidget(QPushButton('2'), 0, Qt.AlignBottom | Qt.AlignRight)layout.addWidget(QPushButton('3'))layout.addWidget(QPushButton('4'), 3, Qt.AlignTop | Qt.AlignJustify) # 伸缩量3, 居中,两边对齐layout.addWidget(QPushButton('5'))layout.setSpacing(5) # 控件间距 5self.setLayout(layout)if __name__ == '__main__':import sysapp = QApplication(sys.argv)win = Winform()win.show()sys.exit(app.exec_())

在这里插入图片描述

addStretch() 添加可伸缩控件
  • 添加到布局末尾
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
import sysclass WindowDemo(QWidget):def __init__(self):super().__init__()btn1 = QPushButton(self)btn2 = QPushButton(self)btn3 = QPushButton(self)btn1.setText('button 1')btn2.setText('button 2')btn3.setText('button 3')layout = QHBoxLayout()# 设置伸缩量为2layout.addStretch(2)layout.addWidget(btn1)# 设置伸缩量为1layout.addStretch(1)layout.addWidget(btn2)# 设置伸缩量为1layout.addStretch(1)layout.addWidget(btn3)# 设置伸缩量为1layout.addStretch(1)# 剩余空间比例 2:1:1:1self.setLayout(layout)self.setWindowTitle("addStretch 例子")if __name__ == "__main__":app = QApplication(sys.argv)win = WindowDemo()win.show()sys.exit(app.exec_())

在这里插入图片描述
若只给最前面添加一个,则为右对齐
若只给最后面添加一个,则为左对齐

4. QGridLayout

  • 控件占一格的例子
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButtonclass Winform(QWidget):def __init__(self, parent=None):super(Winform, self).__init__(parent)self.initUI()def initUI(self):# 1grid = QGridLayout()self.setLayout(grid)# 2names = ['Cls', 'Back', '', 'Close','7', '8', '9', '/','4', '5', '6', '*','1', '2', '3', '-','0', '.', '=', '+']# 3positions = [(i, j) for i in range(5) for j in range(4)]print(positions)# 4for position, name in zip(positions, names):if name == '':continuebutton = QPushButton(name)grid.addWidget(button, *position)self.move(300, 150)self.setWindowTitle('网格布局管理例子')if __name__ == "__main__":app = QApplication(sys.argv)form = Winform()form.show()sys.exit(app.exec_())

在这里插入图片描述

  • 控件跨越多格
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit, QTextEdit, QGridLayout, QApplicationclass Winform(QWidget):def __init__(self, parent=None):super(Winform, self).__init__(parent)self.initUI()def initUI(self):titleLabel = QLabel('标题')authorLabel = QLabel('提交人')contentLabel = QLabel('申告内容')titleEdit = QLineEdit()authorEdit = QLineEdit()contentEdit = QTextEdit()grid = QGridLayout()grid.setSpacing(10)grid.addWidget(titleLabel, 1, 0) # row 1, column 0grid.addWidget(titleEdit, 1, 1) # row 1, column 1grid.addWidget(authorLabel, 2, 0)grid.addWidget(authorEdit, 2, 1)grid.addWidget(contentLabel, 3, 0)grid.addWidget(contentEdit, 3, 1, 5, 1) # 行,列,行高,列宽self.setLayout(grid)self.setGeometry(300, 300, 350, 300)self.setWindowTitle('故障申告')if __name__ == "__main__":app = QApplication(sys.argv)form = Winform()form.show()sys.exit(app.exec_())

在这里插入图片描述

5. QFormLayout

  • 两列,一般左侧是 label,右侧是用户选择或者输入 field
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QLineEdit, QLabelclass Winform(QWidget):def __init__(self, parent=None):super(Winform, self).__init__(parent)self.setWindowTitle("窗体布局管理例子")self.resize(400, 100)fromlayout = QFormLayout()labl1 = QLabel("姓名")lineEdit1 = QLineEdit()labl2 = QLabel("年龄")lineEdit2 = QLineEdit()labl3 = QLabel("性别")lineEdit3 = QLineEdit()fromlayout.addRow(labl1, lineEdit1)fromlayout.addRow(labl2, lineEdit2)fromlayout.addRow(labl3, lineEdit3)self.setLayout(fromlayout)if __name__ == "__main__":app = QApplication(sys.argv)form = Winform()form.show()sys.exit(app.exec_())

在这里插入图片描述

6. 嵌套布局

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout, QPushButtonclass MyWindow(QWidget):def __init__(self):super().__init__()self.setWindowTitle('嵌套布局示例')# 全局布局(1个):水平wlayout = QHBoxLayout()# 局部布局(4个):水平、竖直、网格、表单hlayout = QHBoxLayout()vlayout = QVBoxLayout()glayout = QGridLayout()formlayout = QFormLayout()# 局部布局添加部件(例如:按钮)hlayout.addWidget(QPushButton(str(1)))hlayout.addWidget(QPushButton(str(2)))vlayout.addWidget(QPushButton(str(3)))vlayout.addWidget(QPushButton(str(4)))glayout.addWidget(QPushButton(str(5)), 0, 0)glayout.addWidget(QPushButton(str(6)), 0, 1)glayout.addWidget(QPushButton(str(7)), 1, 0)glayout.addWidget(QPushButton(str(8)), 1, 1)formlayout.addWidget(QPushButton(str(9)))formlayout.addWidget(QPushButton(str(10)))formlayout.addWidget(QPushButton(str(11)))formlayout.addWidget(QPushButton(str(12)))# 准备四个部件hwg = QWidget()vwg = QWidget()gwg = QWidget()fwg = QWidget()# 四个部件设置局部布局hwg.setLayout(hlayout)vwg.setLayout(vlayout)gwg.setLayout(glayout)fwg.setLayout(formlayout)# 四个部件加至全局布局wlayout.addWidget(hwg)wlayout.addWidget(vwg)wlayout.addWidget(gwg)wlayout.addWidget(fwg)# 窗体本体设置全局布局self.setLayout(wlayout)if __name__ == "__main__":app = QApplication(sys.argv)win = MyWindow()win.show()sys.exit(app.exec_())

在这里插入图片描述
上面使用了多个空白控件来设置局部布局

下面只使用一个空白控件

# _*_ coding: utf-8 _*_
# @Time : 2022/6/5 17:11
# @Author : Michael
# @File : nest_layout1.py
# @desc :
import sysfrom PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout, QPushButton, QLineEdit, \QApplicationclass mywin(QWidget):def __init__(self):super().__init__()self.setWindowTitle('nest_layout1')self.resize(500, 300)# 全局部件, 注意 self 参数globalwidget = QWidget(self)# 全局布局globallayout = QHBoxLayout(globalwidget)# 局部布局h_layout = QHBoxLayout()v_layout = QVBoxLayout()g_layout = QGridLayout()form_layout = QFormLayout()# 局部布局 添加控件h_layout.addWidget(QPushButton(str(1)))h_layout.addWidget(QPushButton(str(2)))v_layout.addWidget(QPushButton(str(3)))v_layout.addWidget(QPushButton(str(4)))g_layout.addWidget(QPushButton(str(5)), 0, 0)g_layout.addWidget(QPushButton(str(6)), 0, 1)g_layout.addWidget(QPushButton(str(7)), 1, 0)g_layout.addWidget(QPushButton(str(8)), 1, 1)g_layout.addWidget(QPushButton(str(9)), 2, 0)g_layout.addWidget(QPushButton(str(10)), 2, 1)form_layout.addRow('name', QLineEdit())form_layout.addRow('age', QLineEdit())# 局部布局 添加到 全局布局globallayout.addLayout(h_layout)globallayout.addLayout(v_layout)globallayout.addLayout(g_layout)globallayout.addLayout(form_layout)
if __name__ == '__main__':app = QApplication(sys.argv)win = mywin()win.show()sys.exit(app.exec_())

在这里插入图片描述

7. QSplitter 布局

  • 可以动态拖动子控件之间的边界,默认是横向布局
# _*_ coding: utf-8 _*_
# @Time : 2022/6/5 17:31
# @Author : Michael
# @File : qsplitter.py
# @desc :
import sysfrom PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QFrame, QSplitter, QTextEdit, QApplicationclass qsplitter_demo(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setGeometry(300, 300, 300, 300)self.setWindowTitle('QSplitter')h_layout = QHBoxLayout(self)topleft = QFrame()topleft.setFrameShape(QFrame.StyledPanel)textedit = QTextEdit()spliter1 = QSplitter(Qt.Horizontal)spliter1.addWidget(topleft)spliter1.addWidget(textedit)spliter1.setSizes([100, 200])bottom = QFrame()bottom.setFrameShape(QFrame.StyledPanel)spliter2 = QSplitter(Qt.Vertical)spliter2.addWidget(spliter1)spliter2.addWidget(bottom)h_layout.addWidget(spliter2)self.setLayout(h_layout)
if __name__ == '__main__':app = QApplication(sys.argv)qsplitter_demo = qsplitter_demo()qsplitter_demo.show()sys.exit(app.exec_())

在这里插入图片描述

这篇关于PyQt5 布局管理(水平、垂直、网格、表单、嵌套、QSplitter)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/377313

相关文章

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

Linux内存泄露的原因排查和解决方案(内存管理方法)

《Linux内存泄露的原因排查和解决方案(内存管理方法)》文章主要介绍了运维团队在Linux处理LB服务内存暴涨、内存报警问题的过程,从发现问题、排查原因到制定解决方案,并从中学习了Linux内存管理... 目录一、问题二、排查过程三、解决方案四、内存管理方法1)linux内存寻址2)Linux分页机制3)

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

Spring常见错误之Web嵌套对象校验失效解决办法

《Spring常见错误之Web嵌套对象校验失效解决办法》:本文主要介绍Spring常见错误之Web嵌套对象校验失效解决的相关资料,通过在Phone对象上添加@Valid注解,问题得以解决,需要的朋... 目录问题复现案例解析问题修正总结  问题复现当开发一个学籍管理系统时,我们会提供了一个 API 接口去

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用

使用PyQt5编写一个简单的取色器

《使用PyQt5编写一个简单的取色器》:本文主要介绍PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16进制颜色编码,一款跟随鼠标刷新图像的RGB和16... 目录取色器1取色器2PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16

SpringBoot使用minio进行文件管理的流程步骤

《SpringBoot使用minio进行文件管理的流程步骤》MinIO是一个高性能的对象存储系统,兼容AmazonS3API,该软件设计用于处理非结构化数据,如图片、视频、日志文件以及备份数据等,本文... 目录一、拉取minio镜像二、创建配置文件和上传文件的目录三、启动容器四、浏览器登录 minio五、

IDEA中的Kafka管理神器详解

《IDEA中的Kafka管理神器详解》这款基于IDEA插件实现的Kafka管理工具,能够在本地IDE环境中直接运行,简化了设置流程,为开发者提供了更加紧密集成、高效且直观的Kafka操作体验... 目录免安装:IDEA中的Kafka管理神器!简介安装必要的插件创建 Kafka 连接第一步:创建连接第二步:选