Introduction to PyQt5 (eighteen) layout (on)

table of Contents

1. In 2021, don’t read the past and fear the future

2. Absolute layout

3. Horizontal box layout (QHBoxLayout)

Four. Set the alignment of the control

V. Vertical box layout

Six. Set the amount of expansion and contraction of the layout


1. In 2021, don’t read the past and fear the future

Can't be slumped! Can't be slumped! Can't be slumped! (I tell the important thing three times)

Regardless of the past, please put all your worries behind for the time being in the new year, spend more time with your family, gather with old friends, take a look at your hometown, and let yourself be free. Sometimes I feel like I’ve been typing for a long time and I really feel that my personality has changed, becoming more and more indifferent, more and more wanting to be separated from the surrounding environment, and more and more like being alone. This is good, but it’s not good after a long time. Always play with friends, be a person who truly loves life, and control your temper, not just a cheerful person in front of others.

A word for everyone:

I leave uncultivated today, was precisely yesterday perishestomorrow which person of the body implored.

Translation:  

The day that I leave uncultivated is the tomorrow that those who died yesterday prayed for.

Encourage it!

 

2. Absolute layout

Code:

import sys,math
from PyQt5.QtWidgets import *


class AbsoluteLayout(QWidget):
    def __init__(self):
        super(AbsoluteLayout, self).__init__()
        self.setWindowTitle('绝对布局')
        self.resize(300,200)

        self.label1=QLabel('欢迎',self)
        self.label1.move(15,20)

        self.label2=QLabel('学习',self)
        self.label2.move(35,40)

        self.label2 = QLabel('PyQt5', self)
        self.label2.move(55, 80)

if __name__=='__main__':
    app=QApplication(sys.argv)
    main=AbsoluteLayout()
    main.show()
    sys.exit(app.exec_())

operation result:

 

3. Horizontal box layout (QHBoxLayout)

Code:

import sys,math
from PyQt5.QtWidgets import *


class HBoxLayout(QWidget):
    def __init__(self):
        super(HBoxLayout, self).__init__()
        self.setWindowTitle('水平盒布局')
        self.resize(300,200)

        hlayout=QHBoxLayout()
        hlayout.addWidget(QPushButton('按钮1'))
        hlayout.addWidget(QPushButton('按钮2'))
        hlayout.addWidget(QPushButton('按钮3'))
        hlayout.addWidget(QPushButton('按钮4'))
        hlayout.addWidget(QPushButton('按钮5'))
        #设置控件之间的间距
        hlayout.setSpacing(40)

        self.setLayout(hlayout)


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=HBoxLayout()
    main.show()
    sys.exit(app.exec_())

operation result:

 

Four. Set the alignment of the control

Code:

import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class HBoxLayout(QWidget):
    def __init__(self):
        super(HBoxLayout, self).__init__()
        self.setWindowTitle('水平盒布局')
        self.resize(300,200)

        hlayout=QHBoxLayout()
        '''
        参数二:拉伸量(几个单位),eg:按钮1后面有2个单位的拉伸,按钮2后面有4个单位的拉伸
        参数三:对齐方式,eg:按钮1是按照左上方对齐,其他以此类推
        '''
        hlayout.addWidget(QPushButton('按钮1'),2,Qt.AlignLeft|Qt.AlignTop)
        hlayout.addWidget(QPushButton('按钮2'),4,Qt.AlignLeft|Qt.AlignTop)
        hlayout.addWidget(QPushButton('按钮3'),1,Qt.AlignLeft|Qt.AlignTop)
        hlayout.addWidget(QPushButton('按钮4'),1,Qt.AlignLeft|Qt.AlignBottom)
        hlayout.addWidget(QPushButton('按钮5'),1,Qt.AlignLeft|Qt.AlignBottom)
        #设置控件之间的间距
        hlayout.setSpacing(40)

        self.setLayout(hlayout)


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=HBoxLayout()
    main.show()
    sys.exit(app.exec_())

operation result:

 

V. Vertical box layout

Code:

import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class VBoxLayout(QWidget):
    def __init__(self):
        super(VBoxLayout, self).__init__()
        self.setWindowTitle('垂直盒布局')
        self.resize(300,200)

        hlayout=QVBoxLayout()
        '''
        参数二:拉伸量(几个单位),eg:按钮1后面有2个单位的拉伸,按钮2后面有4个单位的拉伸
        参数三:对齐方式,eg:按钮1是按照左上方对齐,其他以此类推
        '''
        hlayout.addWidget(QPushButton('按钮1'))
        hlayout.addWidget(QPushButton('按钮2'))
        hlayout.addWidget(QPushButton('按钮3'))
        hlayout.addWidget(QPushButton('按钮4'))
        hlayout.addWidget(QPushButton('按钮5'))
        #设置控件之间的间距
        hlayout.setSpacing(40)

        self.setLayout(hlayout)


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=VBoxLayout()
    main.show()
    sys.exit(app.exec_())

operation result:

 

Six. Set the amount of expansion and contraction of the layout

Code 1:

import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Stretch(QWidget):
    def __init__(self):
        super(Stretch, self).__init__()
        self.setWindowTitle('设置伸缩量')
        self.resize(800,100)

        btn1=QPushButton(self)
        btn2 = QPushButton(self)
        btn3 = QPushButton(self)
        btn1.setText('按钮1')
        btn2.setText('按钮2')
        btn3.setText('按钮3')

        layout=QHBoxLayout()
        #伸缩量,在水平布局里有讲过
        layout.addStretch(1) #在按钮前面添加伸缩单位
        layout.addWidget(btn1)

        layout.addStretch(2)
        layout.addWidget(btn2)

        layout.addStretch(3)
        layout.addWidget(btn3)

        self.setLayout(layout)


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=Stretch()
    main.show()
    sys.exit(app.exec_())

Operation result 1:

 

Code 2:

The effect of the following code is that part of the code is on the left and the other part is on the right.

import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Stretch(QWidget):
    def __init__(self):
        super(Stretch, self).__init__()
        self.setWindowTitle('设置伸缩量')
        self.resize(1000,100)

        btn1=QPushButton(self)
        btn2 = QPushButton(self)
        btn3 = QPushButton(self)
        btn4 = QPushButton(self)
        btn5 = QPushButton(self)
        btn1.setText('按钮1')
        btn2.setText('按钮2')
        btn3.setText('按钮3')
        btn4.setText('按钮4')
        btn5.setText('按钮5')

        layout=QHBoxLayout()

        layout.addStretch(0)#伸缩量设置为0,是先排列
        layout.addWidget(btn1)
        layout.addWidget(btn2)
        layout.addWidget(btn3)
        layout.addWidget(btn4)
        layout.addWidget(btn5)

        btnOK=QPushButton(self)
        btnOK.setText('确定')
        btnCancel = QPushButton(self)
        btnCancel.setText('取消')
        layout.addStretch(1)

        layout.addWidget(btnOK)
        layout.addWidget(btnCancel)

        self.setLayout(layout)


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=Stretch()
    main.show()
    sys.exit(app.exec_())

Operation result 2:

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/113793037