PyQt5 layout Daquan

Today, let's take a look at the layout of PyQt5. I used to do Android development, and there are layouts in android. To put it simply, the main

1. Linear layout

2. Relative layout

3, table layout

4. Frame layout

5. Absolute layout

Actually in PyQt5, these are similar

1, the absolute layout of PyQt5

write picture description here

import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication
calss Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        lbl1 = QLabel("你好", self)
        lbl1.move(15, 10)

        lbl2 = QLabel("python", self)
        lbl2.move(35,40)

        lbl3 = QLable("world", self)
        lbl3.move(55, 70)

        self.setGeometry(300, 300, 320, 120)
        self.setWindowTitle("绝对布局")
if __name__ == '__main__':  
    app = QApplication(sys.argv)
    demo = Example()
    demo.show()
    sys.exet(app.exec_())

2. Horizontal layout of PyQt5's linear layout

write picture description here

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtCore import Qt
class WInform(QWidget):
    def __init__(self, parent=None):
        super(Windform, self).__init__(parent)

        hlayout = QHBoxLayout()
        hlayout.addWidget(QPushButton(str(1)))
        hlayout.addWidget(QPushButton(str(2)))
        hlayout.addWidget(QPushButton(str(3)))
        hlayout.addWidget(QpushButton(str(4)))
        hlayout.addWidget(QPushButton(str(5)))
        # 设置控件的间距
        hlayout.setSpaceing(0)
        self.setLayout(hlayout)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Winform()
    form.show()
    sys.exit(app.exec_())

3. Horizontal layout of PyQt5's linear layout (2)

write picture description here

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtCore import Qt
class Winform(QWidget):
    def __init__(self, parent=None):
        super(Winform,self).__init__(parent)
        self.setWindowTitle("水平布局管理")
        self.resize(800,200)

        hlayout = QHbobLayout()
        #按钮水平居左,垂直居上
        hlayout.addWidget(QPushButton(str(1)),0, Qt.AlignLeft|Qt.AlignTop)
        hlayout.addWidget(QPushButton(str(2)),0, Qt.AlignLeft|Qt.AlignTop)
        hlayout.addWidget(QPushButton(str(3),5))#这里5表示的是权重
        #水平居左,垂直居下
        hlayout.addWidget(QPushButton(str(4)),0,Qt.AlignLeft|Qt.AlignBottom)
        hlayout.addWidget(QpushButton(str(5)),0, Qt.ALignLeft|Qt.AlignBottom)
        self.setLayout(hlayout)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Winform()
    form.show()
    sys.exit(app.exec_())

4. Vertical layout of PyQt5's linear layout

write picture description here

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QLineEdit, QLabel
class Winform(QWidget):
    def __init__(self, parent=None):
        super(Winform, self).__init__(parent)
        self.setWindowTitle("垂直布局")
        self.resize(400, 100)

        formLayout = QFormLayout()
        lab1 = QLable("标签1")
        lineEdit1 = QLineEdit()
        lab2 = QLable("标签2")
        lineEdit2 = QLineEdit()
        lab3 = QLable("标签3")
        lineEdit3 = QLineEdit()
        #向布局中添加这几个控件
        formLayout.addRow(labl1, lineEdit1)
        # 一行两个控件,水平摆放
        formLayout.addRow(labl2, lineEdit2)
        formLayout.addRow(lab3, lineEdit3)

        self.setLayout(fromLayout)
if  __name__ =="__main__":
    app = QApplication(sys.argv)
    form = Winform()
    form.show()
    sys.exit(app.exec_())

5, PyQt5 grid layout

write picture description here


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton
class Winform(QWidget):
    def __init__(self, parent=None):
        super(Winform, self).__init__(parent)
        self.initUI()

    def initUI(self):
        grid = QGridLayout()
        self.setLayout(grid)

        names = ['Cls', 'Back', '', 'Close',
                 '7', '8', '9', '/',
                 '4', '5', '6', '*',
                 '1', '2', '3', '-',
                 '0', '.', '=', '+']
        #使用生成器生成具体位置 5 列 4行
        positions = [(i,j) for i in range(5) for j in range(4)]
        for position, name in zip(position, names):
            if name = "":
                continue
             button - QPushButton(name)
             #Python允许你在list或tuple前面加一个*号,把list或tuple的元素变成可变参数传进去
             grid.addwidget(button, *position)
        self.move(300, 150)
        self.setWindowTitle("网格布局管理")
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Winform()
    form.show()
    sys.exit(app.exec_())

5. Grid layout of PyQt5 (2)

write picture description here

class Winform(QWidget):
    def __init__(self, parent=None):
        super(Winform, self).__init__(parent)
        self.initUI()

    def initUI(self):
        titleLable = QLable("标题")
        authorLabel = QLabel('提交人')
        contentLabel = QLabel('申告内容')

        titleEdit = QLineEdit()
        authorEdit = QLineEdit()
        contentEdit = QTextEdit()
        grid = QGridLayout()
        grid.setSpacing(10)

        # 第一个参数表示控件,后面的两个参数表示的是控件所在的位置
        grid.addWidget(titleLable, 1, 0)
        grid.addWidget(titleEdit, 1, 1)

        grid.addWidget(authorLabel, 2, 0)
        grid.addWidget(authorEdit, 2, 1)

        grid.addWidget(contentLable, 3,0)
        # 最后两个参数表示的是 所占的行和所占的列,开发者自己去体会吧
        grid.addWidget(gcontentEdit,3, 1, 5,1)
        self.setLayout(grid)
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle("申请")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Winform()
    form.show()
    sys.exit(app.exec_())

Don't copy it, just type it manually, just understand the idea

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325579970&siteId=291194637