1. 简介
QHBoxLayout和QVBoxLayout是用来布局的,前者用来水平布局,后者用来垂直布局。
水平布局
垂直布局
2.QHBoxLayout
import sys
from PyQt5.QtWidgets import QPushButton, QApplication, QWidget, QHBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
button1 = QPushButton("first")
button2 = QPushButton("second")
button3 = QPushButton("third")
horizontal = QHBoxLayout()
horizontal.addWidget(button1)
horizontal.addWidget(button2)
horizontal.addWidget(button3)
self.setLayout(horizontal)
self.setGeometry(300, 300, 300, 100)
self.setWindowTitle("demo")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
3. QVBoxLayout
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
button1 = QPushButton("first")
button2 = QPushButton("second")
button3 = QPushButton("third")
vertical = QVBoxLayout()
vertical.addWidget(button1)
vertical.addWidget(button2)
vertical.addWidget(button3)
self.setLayout(vertical)
self.setGeometry(300, 300, 300, 100)
self.setWindowTitle("demo")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
4. 综合布局
import sys
from PyQt5.QtWidgets import QPushButton, QApplication, QWidget, QHBoxLayout, QVBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
button1 = QPushButton("button1")
button2 = QPushButton("button2")
button3 = QPushButton("button3")
button4 = QPushButton("button4")
button5 = QPushButton("button5")
button6 = QPushButton("button6")
horizontal1 = QHBoxLayout()
horizontal1.addWidget(button1)
horizontal1.addWidget(button2)
horizontal1.addWidget(button3)
horizontal2 = QHBoxLayout()
horizontal2.addWidget(button4)
horizontal2.addWidget(button5)
horizontal2.addWidget(button6)
vertical = QVBoxLayout()
vertical.addLayout(horizontal1)
vertical.addLayout(horizontal2)
self.setLayout(vertical)
self.setGeometry(300, 300, 300, 100)
self.setWindowTitle("comprehensive")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
5. 伸展因子
import sys
from PyQt5.QtWidgets import QPushButton, QApplication, QWidget, QHBoxLayout, QVBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
button1 = QPushButton("button1")
button2 = QPushButton("button2")
button3 = QPushButton("button3")
button4 = QPushButton("button4")
button5 = QPushButton("button5")
button6 = QPushButton("button6")
horizontal1 = QHBoxLayout()
horizontal1.addStretch(2)
horizontal1.addWidget(button1)
horizontal1.addStretch(3)
horizontal1.addWidget(button2)
horizontal1.addStretch(5)
horizontal1.addWidget(button3)
horizontal2 = QHBoxLayout()
horizontal2.addWidget(button4)
horizontal2.addWidget(button5)
horizontal2.addWidget(button6)
vertical = QVBoxLayout()
vertical.addLayout(horizontal1)
vertical.addLayout(horizontal2)
self.setLayout(vertical)
self.setGeometry(300, 300, 1000, 500)
self.setWindowTitle("comprehensive")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
作者:Beyong
出处:Beyong博客
github地址:https://github.com/beyong2019
本博客中未标明转载的文章归作者Beyong有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。