菜单栏和工具栏,在大多数编程语言中,都会提供对应的布局,而工作区域的,需要用布局管理器。当然,在PyQt5中,大多数组件可用x、y轴坐标进行定位摆放,只是那样要费时费力,还会遇到调整窗口大小时,组件的尺寸和位置不会随之改变的问题,会遇到不同系统上,应用界面不一致的问题。
为此,PyQt5提供了布局管理器QLayout,QLayout本身不是一个容器或一个窗口部件,它是一个屏幕布局的算法。真正实现布局的是QLayout的子类,有按行或列布局的箱子布局QHBoxLayout和QVBoxLayout,有按网格布局的QGridLayout,还有专门用于表单布局的
QFormLayout,它们都支持布局管理器的嵌套使用。
箱子布局
箱子布局QHBoxLayout或QVBoxLayout是最常见的布局管理器,可支持横向布局和纵向布局。QHBoxLayout,按照从左到右的顺序来添加控件; QVBoxLayout按照从上到下的顺序添加控件,它们可互相嵌套,都继承于QBoxLayout类。
调用窗口QWidget或QMainWindow的setLayout(self, QLayout)方法,可将布局管理器添加到窗口中。
查看源代码,可知QVBoxLayout和QHBoxLayout布局管理器,除了在构造时的方向(LeftToRight、TopToBottom)上不同外,其它的均相同,它们常用的方法如下:
方法 | 描述 |
addLayout(self,stretch=0) | 在布局中添加其他布局,使用stretch(伸缩量)进行伸缩,伸缩量默认为0; |
addStretch(int stretch=0) | 方法在布局管理器中添加一个可伸缩的控件,0(默认)为最小值,并且将stretch作为伸缩量添加到布局末尾,stretch参数表示均分的比例; |
addWidget(self,QWidget, stretch: int = 0,alignment) | 方法用于添加控件到布局管理器中,stretch的作用同addStretch(int stretch=0)的是一样的。 |
alignment为对齐方式,值有:
属性 | 描述 |
Qt.AlignLeft | 水平方向居左对齐; |
Qt.AlignRight | 水平方向具有对齐; |
Qt.AlignCenter | 水平方向居中对齐; |
Qt.AlignJustify | 水平方向两端对齐; |
Qt.AlignTop | 垂直方向靠上对齐; |
Qt.AlignBottom | 垂直方向靠下对齐; |
Qt.AlignVCenter | 垂直方向居中对齐; |
addSpacing(self,int) | 用于设置内间距,即设控件之间的间距; |
setContentsMargins(self, int, int, int, int) | 方法可用于设置左、上、右、下的外边距。 |
程序清单:boxlayout.py
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget,
QVBoxLayout, \ QHBoxLayout, QLabel, QLineEdit, QTextEdit, QPushButton
from PyQt5.QtCore import Qt
# 继承QWidget
class BoxLayout(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
vbox = QVBoxLayout()
# 姓名,水平布局
h_box1 = QHBoxLayout()
name_label = QLabel("姓名:")
name_edit = QLineEdit(self)
name_edit.setPlaceholderText("请输入你的名字")
h_box1.addWidget(name_label, alignment=Qt.AlignRight)
h_box1.addWidget(name_edit, stretch=1)
# 地址,水平布局
h_box2 = QHBoxLayout()
address_label = QLabel("地址:")
address_edit = QLineEdit(self)
address_edit.setPlaceholderText("请输入你的地址")
h_box2.addWidget(address_label, alignment=Qt.AlignRight)
h_box2.addWidget(address_edit, stretch=1)
h_box2.setContentsMargins(0, 10, 0, 10) # 外间距
# 备注
remark_edit = QTextEdit(self)
remark_edit.setPlaceholderText("请输入备注信息")
# 按钮
h_box3 = QHBoxLayout()
submit_btn = QPushButton("确定")
clear_btn = QPushButton("清空")
h_box3.addStretch(1) # 水平布局,填充前面为空
h_box3.addWidget(submit_btn, stretch=0)
h_box3.addSpacing(10) # 添加内间距
h_box3.addWidget(clear_btn, stretch=0)
# 添加到垂直布局里
vbox.addLayout(h_box1)
vbox.addLayout(h_box2)
vbox.addWidget(remark_edit, stretch=1)
vbox.addLayout(h_box3)
self.setLayout(vbox)
# 调整窗口大小
self.resize(900, 500)
# 窗口居中
self.center()
# 窗口标题
self.setWindowTitle("箱子布局")
# 显示窗口
self.show()
# 实现居中
def center(self):
f = self.frameGeometry()
c = QDesktopWidget().availableGeometry().center()
f.moveCenter(c)
self.move(f.topLeft())
if __name__ == "__main__":
app = QApplication(sys.argv)
w = BoxLayout()
sys.exit(app.exec_())
运行程序之后,弹出的窗口如下:
好了,箱子布局的内容就说到这了,关注我,下一节更精彩。
码字不易,你的关注和转发是对我最大的鼓励,谢谢!
一个当了10年技术总监的老家伙,分享多年的编程经验。想学编程的朋友,关注我,你就赚到了。我正在分享Python、前端、Java和App方面的干货了。赶紧来围观啊!!!