1、QBoxLayout修改方向:提供水平或者垂直方向的布局管理器。
修改方向:每隔一秒调整布局的方向。
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QBoxLayou的学习")
self.resize(500, 500)
self.setup_ui()
def setup_ui(self):
label1 = QLabel("标签1")
label1.setStyleSheet("background-color:cyan")
label2 = QLabel("标签2")
label2.setStyleSheet("background-color:yellow")
label3 = QLabel("标签3")
label3.setStyleSheet("background-color:red")
label4 = QLabel("标签4")
label4.setStyleSheet("background-color:orange")
layout = QBoxLayout(QBoxLayout.LeftToRight)
self.setLayout(layout)
layout.addWidget(label1)
layout.addWidget(label2)
layout.addWidget(label3)
layout.addWidget(label4)
timer = QTimer(self)
def test():
layout.setDirection((layout.direction() + 1) % 4)
timer.timeout.connect(test)
timer.start(1000)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Window()
window.resize(500, 500)
window.show()
sys.exit(app.exec_())
添加元素:
# 在索引为1的位置添加控件,插入控件
layout.insertWidget(1, label4)
# 插入布局
layout.insertLayout(2, h_layout)
# 移除控件,指从布局管理器中移除,但控件本身仍然存在
layout.removeWidget(label1)
label2.hide()
label2.show()
移除控件,指从布局管理器中移除,但控件本身仍然存在,hide()方法也会退出布局管理。但之后在使用show仍然会参加布局.
2、添加空白、添加弹簧(伸缩因子)
空白:只占据一定的尺寸和一定的位置。
只想两个单独的控件之间位置变大,可以在他们之间添加空白。(label1和label2之间添加100间隔)
layout.addWidget(label1)
layout.addSpacing(100)
layout.addWidget(label2)
layout.addWidget(label3)
# 空白地方不用算索引
layout.insertSpacing(4, 300)
layout.addWidget(label4)
伸缩因子:多余的空白部分如何分配。(空间足够大的情况按比例分配,但是拥挤的情况下会优先保证控件的参数,可以压缩空白部分的比例)
# 设置伸缩因子
layout.addWidget(label1, 1)
# 添加空白的伸缩因子
layout.addStretch(2)
伸缩因子修改:(某一个控件设置伸缩因子,如果该控件不在布局之中,则返回值为False,在则返回值为False)
其余的伸缩因子默认都为0,label2设置伸缩因子为1时,所有的额外空白都会分配给伸缩因子为1的控件,只有他会根据父控件的调整而调整。
# 设置伸缩因子
layout.addWidget(label1)
#layout.addSpacing(100)
layout.addWidget(label2)
# 添加空白的伸缩因子,默认伸缩因子为0
layout.addStretch()
layout.setStretchFactor(label2, 1)
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QBoxLayou的学习")
self.resize(500, 500)
self.setup_ui()
def setup_ui(self):
label1 = QLabel("标签1")
label1.setStyleSheet("background-color:cyan")
label2 = QLabel("标签2")
label2.setStyleSheet("background-color:yellow")
label3 = QLabel("标签3")
label3.setStyleSheet("background-color:red")
label4 = QLabel("标签4")
label4.setStyleSheet("background-color:orange")
layout = QBoxLayout(QBoxLayout.TopToBottom)
self.setLayout(layout)
# 设置伸缩因子
# layout.addWidget(label1)
# #layout.addSpacing(100)
# layout.addWidget(label2)
# # 添加空白的伸缩因子,默认伸缩因子为0
# layout.addStretch()
#layout.setStretchFactor(label2, 1)
# layout.addWidget(label3)
# layout.addWidget(label4)
label5 = QLabel("标签5")
label5.setStyleSheet("background-color:pink")
label6 = QLabel("标签6")
label6.setStyleSheet("background-color:blue")
label7 = QLabel("标签7")
label7.setStyleSheet("background-color:cyan")
h_layout = QBoxLayout(QBoxLayout.RightToLeft)
h_layout.addWidget(label5)
h_layout.addWidget(label6, 1)
h_layout.addWidget(label7)
# 3、把需要布局的子控件添加到布局管理器中
layout.addWidget(label1)
layout.addLayout(h_layout, 1)
layout.addWidget(label2)
layout.addWidget(label3, 0)
# 在索引为1的位置添加控件,插入控件
# layout.insertWidget(1, label4)
# label5 = QLabel("标签5")
# label5.setStyleSheet("background-color:pink")
# label6 = QLabel("标签6")
# label6.setStyleSheet("background-color:blue")
# label7 = QLabel("标签7")
# label7.setStyleSheet("background-color:cyan")
# h_layout = QBoxLayout(QBoxLayout.RightToLeft)
# h_layout.addWidget(label5)
# h_layout.addWidget(label6)
# h_layout.addWidget(label7)
#
# # 插入布局
# layout.insertLayout(2, h_layout)
# 移除控件,指从布局管理器中移除,但控件本身仍然存在
# layout.removeWidget(label1)
#
# label2.hide()
# label2.show()
# timer = QTimer(self)
#
# def test():
# layout.setDirection((layout.direction() + 1) % 4)
# timer.timeout.connect(test)
# timer.start(1000)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Window()
window.resize(500, 500)
window.show()
sys.exit(app.exec_())