1. 水平布局管理器(QHBoxLayout
)
用于水平排列子组件,子组件按照从左到右的顺序水平排列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import sys
from PySide6.QtWidgets import QApplication, QHBoxLayout, QWidget, QPushButton, QVBoxLayout
class MyWidget(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle("Cyber psychosis") self.resize(400, 200) self.setLayout(QHBoxLayout()) self.setup_ui()
def setup_ui(self): button1 = QPushButton('Button 1') button2 = QPushButton('Button 2')
layout = self.layout() layout.addWidget(button1) layout.addWidget(button2)
if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec())
|
2. 垂直布局管理器(QVBoxLayout
)
用于垂直排列子组件,按照从上到下的顺序垂直排列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import sys
from PySide6.QtWidgets import QApplication, QHBoxLayout, QWidget, QPushButton, QVBoxLayout
class MyWidget(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle("Cyber psychosis") self.resize(400, 200) self.setLayout(QVBoxLayout()) self.setup_ui()
def setup_ui(self): button1 = QPushButton('Button 1') button2 = QPushButton('Button 2')
layout = self.layout() layout.addWidget(button1) layout.addWidget(button2)
if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec())
|
3. 网格布局管理器(QGridLayout
)
用于将子组件放置在一个网格中,每个子组件可以占据一个或多个网格单元
这个管理器相当于把窗口划分成多个网格,就像excel表格那样,每个格子可以放一个子组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import sys
from PySide6.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QWidget, QPushButton, QVBoxLayout from PySide6.QtCore import Qt
class MyWidget(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle("Cyber psychosis") self.resize(400, 200) self.setLayout(QGridLayout()) self.setup_ui()
def setup_ui(self): button1 = QPushButton('Button 1') button2 = QPushButton('Button 2') button3 = QPushButton('Button 3')
layout = self.layout() layout.addWidget(button1, 0, 0) layout.addWidget(button2, 0, 1) layout.addWidget(button3, 1, 0, 1, 2)
layout.setSpacing(10)
layout.setAlignment(button1, Qt.AlignTop | Qt.AlignLeft)
layout.setRowStretch(0, 1) layout.setColumnStretch(1, 1)
if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec())
|
可以使用以下标志位来设置对齐方式:
水平对齐标志(Qt.AlignmentFlag):
Qt.AlignLeft
:左对齐Qt.AlignRight
:右对齐Qt.AlignHCenter
:水平居中Qt.AlignJustify
:两端对齐(在水平方向上拉伸以填充网格单元)
垂直对齐标志(Qt.AlignmentFlag):
Qt.AlignTop
:顶部对齐Qt.AlignBottom
:底部对齐Qt.AlignVCenter
:垂直居中
这些标志位可以通过按位或运算来组合使用,例如 Qt.AlignTop | Qt.AlignLeft
表示左上角对齐。
用于将标签和输入字段配对显示,适用于创建表单式的用户界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import sys
from PySide6.QtWidgets import QApplication, QFormLayout, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QWidget, \ QPushButton, \ QVBoxLayout from PySide6.QtCore import Qt
class MyWidget(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle("Cyber psychosis") self.setLayout(QFormLayout()) self.setup_ui()
def setup_ui(self): label1 = QLabel('Name:') line_edit1 = QLineEdit()
label2 = QLabel('Age:') line_edit2 = QLineEdit()
layout = self.layout() layout.addRow(label1, line_edit1) layout.addRow(label2, line_edit2)
layout.setHorizontalSpacing(15) layout.setVerticalSpacing(10)
if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec())
|
4. 堆叠布局管理器(QStackedLayout
)
用于在同一位置显示多个子组件,但只显示其中一个。
常用于实现选项卡式的界面或者在同一区域切换显示不同的组件。
举个例子来说就一个萝卜坑种了好几个种子,我想让哪个种子出来就让哪个出来,但是也只能有一个种子长出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import sys
from PySide6.QtWidgets import QApplication, QLabel, QLineEdit, QStackedLayout, \ QWidget, \ QPushButton, \ QVBoxLayout from PySide6.QtCore import Qt
class MyWidget(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle("Cyber psychosis") self.setLayout(QStackedLayout()) self.setup_ui()
def setup_ui(self): button1 = QPushButton('Page 1') label = QLabel('Page 2')
layout = self.layout()
layout.addWidget(button1) layout.addWidget(label)
layout.setCurrentIndex(0)
button1.clicked.connect(lambda: layout.setCurrentIndex(1))
if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec())
|
[{"url":"https://img.pupper.cn/img/1725962240116.png","alt":""},{"url":"https://img.pupper.cn/img/1725962268327.png","alt":""}]