【PyQt5】详解QHBoxLayout和QVBoxLayout

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有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。

  • 31
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PyQt中的QHBoxLayout和QVBoxLayout都是布局管理器,可以用于自动管理窗口中控件的位置和大小。它们的区别在于: - QHBoxLayout用于管理水平方向上的控件,控件从左到右依次排列; - QVBoxLayout用于管理垂直方向上的控件,控件从上到下依次排列。 简单来说,如果你需要在水平方向上排列控件,就可以使用QHBoxLayout;如果你需要在垂直方向上排列控件,就可以使用QVBoxLayout。 以下是一个QHBoxLayout和QVBoxLayout的示例代码: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QLabel class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): label1 = QLabel("Label 1", self) label2 = QLabel("Label 2", self) label3 = QLabel("Label 3", self) hbox = QHBoxLayout() # 创建一个 QHBoxLayout 对象 hbox.addWidget(label1) # 将 label1 控件添加到 QHBoxLayout 中 hbox.addWidget(label2) # 将 label2 控件添加到 QHBoxLayout 中 hbox.addWidget(label3) # 将 label3 控件添加到 QHBoxLayoutvbox = QVBoxLayout() # 创建一个 QVBoxLayout 对象 vbox.addStretch(1) # 添加一个伸缩量 vbox.addLayout(hbox) # 将 QHBoxLayout 对象添加到 QVBoxLayoutvbox.addStretch(1) # 添加一个伸缩量 self.setLayout(vbox) # 将 QVBoxLayout 对象设置为窗口的布局管理器 self.setGeometry(300, 300, 300, 150) # 设置窗口的位置和大小 self.setWindowTitle('HBoxLayout and QVBoxLayout') # 设置窗口的标题 self.show() # 显示窗口 if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 在这个例子中,我们创建了一个窗口,并在窗口中添加了三个标签:Label 1、Label 2、Label 3。我们使用QHBoxLayout来管理这三个标签的位置和大小,将它们从左至右依次排列。然后,我们使用QVBoxLayout来管理QHBoxLayout对象和两个伸缩量的位置和大小,将它们从上至下依次排列。最后,我们将QVBoxLayout对象设置为窗口的布局管理器,这样可以确保这三个标签和两个伸缩量始终保持在正确的位置。如果你运行这个例子,你将看到一个简单的窗口,其中有三个标签,它们被垂直地排列在一起,两侧有伸缩量。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值