PyQT5基础布局管理

绝对定位布局

使用move(x, y)可以对窗口进行布局,以窗口左上角为原点,向右为 x 轴正方向,向下为 y 轴正方向,移动(x,y);

import sys

from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QWidget, QLabel, QApplication


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        font=QFont("微软雅黑",12)
        lbl1 = QLabel('java', self)
        lbl1.move(15, 10)

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

        lbl3 = QLabel('c++', self)
        lbl3.move(55, 70)

        lbl1.setFont(font)
        lbl2.setFont(font)
        lbl3.setFont(font)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Program Language')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

横向布局和纵向布局

使用QHBoxLayoutQVBoxLayout,来分别创建横向布局和纵向布局

addStretch():Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.

addStretch()添加弹簧伸缩量,如在按钮左侧添加弹簧,则拉伸时按钮会贴近右边界。两侧添加弹簧量相同,则拉伸时居中。弹簧其实是平分了空白区域。

setGeometry (0, 0, 30, 35) 四个参数:
从屏幕上(0,0)位置开始(即为最左上角的点),显示一个30*35的界面(宽30,高35)。

import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
                             QHBoxLayout, QVBoxLayout, QApplication)


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        rightButton = QPushButton("Right")

        qhLayout = QHBoxLayout()
        qvLayout = QVBoxLayout()

        qhLayout.addStretch(1)

        qhLayout.addWidget(okButton)
        qhLayout.addWidget(cancelButton)
        qvLayout.addStretch(1)
        qvLayout.addLayout(qhLayout)
        qvLayout.addWidget(rightButton)

        self.setLayout(qvLayout)

        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('Buttons')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

表格布局

import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,
                             QPushButton, QApplication)


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        grid = QGridLayout()
        self.setLayout(grid)

        names = ['X', 'Y', '', 'Z',
                 '(1,0)', '(1,1)', '(1,2)', '(1,3)',
                 '(2,0)', '(2,1)', '(2,2)', '(2,3)',
                 '(3,0)', '(3,1)', '(3,2)', '(3,3)',
                 '(4,0)', '(4,1)', '(4,2)', '(4,3)']

        positions = [(i, j) for i in range(5) for j in range(4)]

        for position, name in zip(positions, names):

            if name == '':
                continue
            button = QPushButton(name)
            grid.addWidget(button, *position)

        self.move(300, 150)

        self.setWindowTitle('Demo')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

原文地址:https://www.cnblogs.com/aslanvon/p/15039695.html