5.PyQt5【布局组件】垂直布局-QVBoxLayout

一、前言

本节我们介绍布局组件中的垂直布局QVBoxLayout。

二、学习目标

1.QVBoxLayout垂直布局应用

三、知识点

1.【QVBoxLayout垂直布局应用】

垂直布局将多个控件在垂直方向排列,控件之间的间隔相同。

  • 添加步骤

    1)创建垂直布局实例:QVBoxLayout()

    2)创建子组件,如按钮组件

    3)为布局添加子组件:addWidget()

    4)为窗体设置布局:setLayout()

  • 代码示例

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
    
    
    class QmyWidget(QWidget):
    
        def __init__(self, parent=None):
            super().__init__(parent)  # 调用父类的构造函数,创建QWidget窗体
            self.setupUi()
    
        def setupUi(self):
            """页面初始化"""
            # 1.创建垂直布局实例
            self.layout = QVBoxLayout()
            # 2.创建子组件,如按钮组件
            self.button1 = QPushButton("Button1")
            self.button2 = QPushButton("Button2")
            self.button3 = QPushButton("Button3")
            self.button4 = QPushButton("Button4")
            # 3.为布局添加子组件
            self.layout.addWidget(self.button1)
            self.layout.addWidget(self.button2)
            self.layout.addWidget(self.button3)
            self.layout.addWidget(self.button4)
            # 4.为窗体设置布局
            self.setLayout(self.layout)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        myMain = QmyWidget()
        myMain.show()
        sys.exit(app.exec_())
    
posted @ 2023-01-16 10:32  测开星辰  阅读(464)  评论(0)    收藏  举报