PyQt5的界面布局主要有两种方法:绝对定位和局部类。在PyQt5中有四种布局方式:水平布局、垂直布局、网格布局、表单布局。还有两种布局方法:addLayout和addWidget,其中addLayout用于在布局中插入子布局,addWidget用于在布局中插入控件。
布局对齐方式:
参数 | 描述 |
---|---|
QtCore.Qt.AlignLeft | 水平方向居左对齐 |
QtCore.Qt.AlignRight | 水平方向居右对齐 |
QtCore.Qt.AlignCenter | 水平方向居中对齐 |
QtCore.Qt.AlignJustify | 水平方向两端对齐 |
QtCore.Qt.AlignTop | 垂直方向靠上对齐 |
QtCore.Qt.AlignBottom | 垂直方向靠下对齐 |
QtCore.Qt.AlignVCenter | 垂直方向居中对齐 |
addSpacing(self, int) 设置各控件的上下间距,通过该方法可以增加额外的控件
addStretch(self, int) 分配布局大小比例
insertStretch(index, stretch = 0) 在指定控件间隔处添加布局比例
insertSpacing(index, size) 在指定控件间隔处设置间隔大小
addStretch是按照比例来调整界面布局,在页面布局中使用广泛,所以我们要使用一定的篇幅来进行代码测试。 使用addStretch,我们可以实现各种对齐方式,而且更加灵活。
import sys
from PyQt5.QtWidgets import QtWidget, QPushButton, QHBoxLayout, QVBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QHBoxLayout()
layout.addWidget(QPushButton(str(1))
layout.addWidget(QPushButton(str(2))
self.setLayout(layout)
self.setGeometry(300, 300, 400, 100)
self.setWindowTitle("Buttons")
if __name__ == "__main__":
app = QtWidget.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
默认是所有控件撑满整个布局文件
图1
def initUI(self):
layout = QHBoxLayout()
layout.addWidget(QPushButton(str(1))
layout.addWidget(QPushButton(str(2))
layout.addStretch(1) # 新增这一行
......
在两个控件后增加这一行,相当于水平布局中存在:按钮1-按钮2-stretch,此时addStretch的参数只要大于0,则表示占满整个布局最后一部分,前面的控件显示为正常大小,不要拉伸。
图2
def initUI(self):
layout = QHBoxLayout()
layout.addStretch(1) # 新增这一行在控件前面
layout.addWidget(QPushButton(str(1))
layout.addWidget(QPushButton(str(2))
......
这一行加入到布局中所有控件之前,相当于水平布局中存在:stretch-按钮1-按钮2,表示占满整个布局的最开始部分,后面的控件显示为正常大小,不要拉伸。
图3
def initUI(self):
layout = QHBoxLayout()
layout.addStretch(1) # 前面增加一行
layout.addWidget(QPushButton(str(1))
layout.addWidget(QPushButton(str(2))
layout.addStretch(1) #后面增加一行
......
stretch-按钮1-按钮2-stretch,表示左右两边充满,控件占据中间位置
图4
注意:当一个布局中出现多个addStretch时,后面的参数就有意义了,其表示整个布局的大小减去控件总大小进行n等份分配。例如:
def initUI(self):
layout = QHBoxLayout()
layout.addStretch(1) # 注意1
layout.addWidget(QPushButton(str(1))
layout.addWidget(QPushButton(str(2))
layout.addStretch(3) # 注意2
表示除去控件1、控件2的宽度,剩余部分四等份,前面占据一份,最后面占据三份
图5
def initUI(self):
layout = QHBoxLayout()
layout.addWidget(QPushButton(str(1))
layout.addStretch(1) # 添加行
layout.addWidget(QPushButton(str(2))
图6
def initUI(self):
layout = QVBoxLayout()
layout.addWidget(QPushButton(str(1))
layout.addWidget(QPushButton(str(2))
layout.addStretch(1) # 添加行
图7
def initUI(self):
layout = QVBoxLayout()
layout.addStretch(1) # 添加行
layout.addWidget(QPushButton(str(1))
layout.addWidget(QPushButton(str(2))
图8
def initUI(self):
layout = QVBoxLayout()
layout.addStretch(1) # 添加行
layout.addWidget(QPushButton(str(1))
layout.addWidget(QPushButton(str(2))
layout.addStretch(1) # 添加行
图9
def initUI(self):
layout = QVBoxLayout()
layout.addWidget(QPushButton(str(1))
layout.addStretch(1) # 添加行
layout.addWidget(QPushButton(str(2))
图10
如果要进行组合布局,例如左上角、右下角等,如下:
def initUI(self):
layout1 = QHBoxLayout()
layout1.addWidget(QPushButton(str(1))
layout1.addWidget(QPushButton(str(2))
layout1.addStretch(1) # 水平居左
layout2 = QVBoxLayout()
layout2.addLayout(layout1)
layout2.addStretch(1) # 垂直顶部对齐
self.setLayout(layout2)
......
图11
addSpacing是设置控件之间的间距。就按照图1的布局及代码进行演示。
def initUI(self):
layout = QHBoxLayout()
layout.addWidget(QPushButton(str(1))
layout.addSpacing(100)
layout.addWidget(QPushButton(str(2))
图12
间距设置可以放置在任何地方,对于调整控件位置十分有效。相当于在控件之间添加了一个空的控件。详细的用法与addStrech类似,参考以上使用即可。