'''
QHBoxLayout()水平布局管理器
QVBoxLayout()水平布局管理器
addWidget(widget): 向布局中添加一个控件。
addLayout(layout): 向布局中添加另一个布局。
设置间距:
setSpacing(space): 设置布局中各部件之间的间距。
setContentsMargins(left,top,right,bottom) :设置边距
addStretch() 设置扩展银子
插入控件或布局:
setStretchFactor(widget, stretch): 设置某个控件的伸展因子,以控制其在布局中的相对大小。
'''
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.resize(500, 300)
self.setWindowTitle("水平布局和垂直布局")
label1 = QLabel("Label1",styleSheet="background-color:cyan")
label2 = QLabel("Label2", styleSheet="background-color:red")
label3 = QLabel("Label3", styleSheet="background-color:yellow")
label4 = QLabel("Label4",styleSheet="background-color:blue")
label5 = QLabel("Label5", styleSheet="background-color:green")
label6 = QLabel("Label6", styleSheet="background-color:gray")
label7 = QLabel("Label7", styleSheet="background-color:black")
# label1.setGeometry(30, 50, 80, 40)
# label2.setGeometry(160, 50, 80, 40)
# label3.setGeometry(290, 50, 80, 40)
# label4.setGeometry(420, 50, 80, 40)
#1 创建水平布局管理器
layout = QHBoxLayout()
#2 添加控件到布局器中
layout.addWidget(label1,)
# layout.addSpacing(50)
layout.addWidget(label2,)
layout.addWidget(label3,)
layout.addWidget(label4,)
# layout.addStretch(1)
#3 设置布局管理器
# layout.setSpacing(80)
# layout.setContentsMargins(50,60,70,80)
v_layout = QVBoxLayout()
v_layout.addWidget(label5)
v_layout.addWidget(label6)
v_layout.addWidget(label7)
v_layout.setStretchFactor(label5,1)
v_layout.setStretchFactor(label6,2)
v_layout.setStretchFactor(label7,2)
layout.addLayout(v_layout)
self.setLayout(layout)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())


承担因您的行为而导致的法律责任,
本站有权保留或删除有争议评论。
参与本评论即表明您已经阅读并接受
上述条款。