栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【PyQt5】详解QHBoxLayout和QVBoxLayout

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【PyQt5】详解QHBoxLayout和QVBoxLayout

文章目录
  • 1. 简介
  • 2.QHBoxLayout
  • 3. QVBoxLayout
  • 4. 综合布局
  • 5. 伸展因子

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286566.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号