[pyqt5 interface development-4] Vertical layout/horizontal layout + 'matryoshka' layout

Table of contents

1. Vertical layout

Second, the combination of the layout

3. Horizontal layout + vertical layout (matryoshka)


1. Vertical layout

Requires module: QVBoxLayout

        # 垂直布局
        layout = QVBoxLayout()

        ………………

        # 应用设置的布局器
        self.setLayout(layout)

Retractors between modules (can be understood as springs, added between components, which position becomes larger)

Note: Adding numbers in brackets can be used to distinguish the ratio of different springs

layout.addStretch()

The code is encapsulated into the class (the previous code is written directly)

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit, QDesktopWidget, QVBoxLayout


class Windows(QWidget):
    def __init__(self):
        # 调用父类的__init__方法(并传入子类,或实例)
        super(Windows, self).__init__()

        # 设置大小
        self.resize(700, 500)
        self.setWindowTitle("pyqt5程序")  # 窗口标题

        # 垂直布局
        layout = QVBoxLayout()

        # 按钮
        btn1 = QPushButton('账号')
        layout.addWidget(btn1)      # 添加到布局器
        btn2 = QPushButton('密码')
        layout.addWidget(btn2)      # 添加到布局器
        btn3 = QPushButton('确认')
        layout.addWidget(btn3)      # 添加到布局器

        layout.addStretch(2)

        # 应用设置的布局器
        self.setLayout(layout)


if __name__ == '__main__':
    # 接收参数(仅有一个)
    app = QApplication(sys.argv)

    w = Windows()       # 创建一个窗口(继承了QWindows,并封装为一个类)
    w.show()            # 展示窗口
    sys.exit(app.exec_())  # 程序进入循环等待,并在退出时关闭应用



Second, the combination of the layout

Vertical Placer + Horizontal Placer

Relationship: Matryoshka

Confirm the vertical and horizontal relationship of multiple layouters in a large layouter ----> and then deal with the layouters in the nesting doll separately

Notice:

For the dolls in the doll ---> put it in a group, and then set the layout of the group

Group required module: QGroupBox

Horizontal + vertical module: QHBoxLayout, QVBoxLayout

Set the group: group1 = QGroupBox()



3. Horizontal layout + vertical layout (matryoshka)

code:

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit, QDesktopWidget, QHBoxLayout, QVBoxLayout, QGroupBox, QRadioButton


class Windows(QWidget):
    def __init__(self):
        # 调用父类的__init__方法(并传入子类,或实例)
        super(Windows, self).__init__()
        self.init_ui()

    def init_ui(self):
        # 先定义最外层的垂直布局(水平布局是QHBoxLayout):垂直布局+水平布局-->垂直关系
        container = QVBoxLayout()       # 最外层布局器(命名为容器)

        #——————————创建第一个组(垂直布局)——————————#
        group1_box = QGroupBox('target')    # 组
        target_layout = QVBoxLayout()   # 布局

        # 控件
        btn1 = QRadioButton('target1')
        btn2 = QRadioButton('target2')
        btn3 = QRadioButton('target3')
        btn4 = QPushButton('确认')

        # 将控件添加到布局中
        target_layout.addWidget(btn1)
        target_layout.addWidget(btn2)
        target_layout.addWidget(btn3)
        target_layout.addWidget(btn4)

        # 将布局设置到组中
        group1_box.setLayout(target_layout)




        # ——————————创建第二个组(水平布局)——————————#
        group2_box = QGroupBox('test')  # 组
        test_layout = QHBoxLayout()   # 布局

        # 控件
        btn1 = QRadioButton('test1')
        btn2 = QRadioButton('test2')
        btn3 = QRadioButton('test3')


        # 将控件添加到布局中
        test_layout.addWidget(btn1)
        test_layout.addWidget(btn2)
        test_layout.addWidget(btn3)

        # 将布局设置到组中
        group2_box.setLayout(test_layout)



        # 将2个组中添加到最外层布局器中
        container.addWidget(group1_box)
        container.addWidget(group2_box)

        # 设置显示最外层布局器 (布局器的应用)
        self.setLayout(container)

        # 窗口设置
        self.resize(700, 500)            # 设置大小
        self.setWindowTitle("pyqt5程序")  # 窗口标题



if __name__ == '__main__':
    app = QApplication(sys.argv)    # 接收参数(仅有一个)
    w = Windows()           # 创建一个窗口(继承了QWindows,并封装为一个类)
    w.show()                # 展示窗口
    sys.exit(app.exec_())   # 程序进入循环等待,并在退出时关闭应用

Guess you like

Origin blog.csdn.net/qq_53079406/article/details/132476563