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

相关文章

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

Spring中管理bean对象的方式(专业级说明)

《Spring中管理bean对象的方式(专业级说明)》在Spring框架中,Bean的管理是核心功能,主要通过IoC(控制反转)容器实现,下面给大家介绍Spring中管理bean对象的方式,感兴趣的朋... 目录1.Bean的声明与注册1.1 基于XML配置1.2 基于注解(主流方式)1.3 基于Java

基于Python+PyQt5打造一个跨平台Emoji表情管理神器

《基于Python+PyQt5打造一个跨平台Emoji表情管理神器》在当今数字化社交时代,Emoji已成为全球通用的视觉语言,本文主要为大家详细介绍了如何使用Python和PyQt5开发一个功能全面的... 目录概述功能特性1. 全量Emoji集合2. 智能搜索系统3. 高效交互设计4. 现代化UI展示效果

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

Mysql中的用户管理实践

《Mysql中的用户管理实践》:本文主要介绍Mysql中的用户管理实践,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录13. 用户管理13.1 用户 13.1.1 用户信息 13.1.2 创建用户 13.1.3 删除用户 13.1.4 修改用户

Python+PyQt5实现MySQL数据库备份神器

《Python+PyQt5实现MySQL数据库备份神器》在数据库管理工作中,定期备份是确保数据安全的重要措施,本文将介绍如何使用Python+PyQt5开发一个高颜值,多功能的MySQL数据库备份工具... 目录概述功能特性核心功能矩阵特色功能界面展示主界面设计动态效果演示使用教程环境准备操作流程代码深度解

linux服务之NIS账户管理服务方式

《linux服务之NIS账户管理服务方式》:本文主要介绍linux服务之NIS账户管理服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、所需要的软件二、服务器配置1、安装 NIS 服务2、设定 NIS 的域名 (NIS domain name)3、修改主

Python+PyQt5实现文件夹结构映射工具

《Python+PyQt5实现文件夹结构映射工具》在日常工作中,我们经常需要对文件夹结构进行复制和备份,本文将带来一款基于PyQt5开发的文件夹结构映射工具,感兴趣的小伙伴可以跟随小编一起学习一下... 目录概述功能亮点展示效果软件使用步骤代码解析1. 主窗口设计(FolderCopyApp)2. 拖拽路径