PyQt5 教程 - 网格布局

Jinku Hu 2023年1月30日 2018年8月22日
  1. PyQt5 网格布局
  2. PyQt5 网格布局跨度
  3. PyQt5 网格布局行列延伸
PyQt5 教程 - 网格布局

我们将在本教程中学习 PyQt5 中的另一种布局方法 - 网格布局 GridLayout

PyQt5 网格布局

QGridLayout 类在窗口网格中放置控件。QGridLayout 获取可用空间并将其划分为行和列,然后将每个窗口控件放入指定的单元格中。

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

class basicWindow(QWidget):
    def __init__(self):
        super().__init__()
        grid_layout = QGridLayout()
        self.setLayout(grid_layout)

        for x in range(3):
            for y in range(3):
                button = QPushButton(str(str(3*x+y)))
                grid_layout.addWidget(button, x, y)
        
        self.setWindowTitle('Basic Grid Layout')

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

在这里,

grid_layout = QGridLayout()

定义了 grid_layoutQWidgets 中的 QGridLayout-网格布局。

self.setLayout(grid_layout)

它将类布局设置为刚定义的布局方法 - 网格布局。

grid_layout.addWidget(button, x, y)

QGridLayout 类的 addWidget 方法将按钮 button 放在坐标为 x,y 的单元格上。左上角的坐标默认为 (0,0)

PyQt5 网格布局

PyQt5 网格布局跨度

通常,每个控件都占用网格的一个单元格,但控件也可以使用 addWidget() 重载方法,将行和列跨越更多数量的单元格。

addWidget (self, QWidget, row, column, rowSpan, columnSpan, Qt.Alignment alignment = 0)

第三个和第四个参数是行和列跨越,默认情况下等于 1。如果跨度为-1,则单元格控件将延伸到右边或底边。

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

class basicWindow(QWidget):
    def __init__(self):
        super().__init__()
        grid_layout = QGridLayout()
        self.setLayout(grid_layout)

        button = QPushButton('1-3')
        grid_layout.addWidget(button, 0, 0, 1, 3)
        
        button = QPushButton('4, 7')
        grid_layout.addWidget(button, 1, 0, -1, 1)
        
        for x in range(1, 3):
            for y in range(1, 3):
                button = QPushButton(str(str(3*x+y)))
                grid_layout.addWidget(button, x, y)
        
        self.setWindowTitle('Basic Grid Layout')

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

PyQt5 网格布局跨度

PyQt5 网格布局行列延伸

setColumnStretchsetRowStretch 设置列或行的伸展因子。伸展因子是与其他列/行相比的相对数。具有较高因子的列/行占用更多可用空间。

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

class basicWindow(QWidget):
    def __init__(self):
        super().__init__()
        grid_layout = QGridLayout()
        self.setLayout(grid_layout)

        for x in range(3):
            for y in range(3):                
                button = QPushButton(str(str(3*x+y)))
                
                grid_layout.addWidget(button, x, y)
                
            grid_layout.setColumnStretch(x, x+1)
            
                
        self.setWindowTitle('Basic Grid Layout')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    windowExample = basicWindow()
    windowExample.show()
    sys.exit(app.exec_())
    
grid_layout.setColumnStretch(x, x+1)

它将三行的伸展因子分别设置为 1,2,3,因此,列宽的顺序为 第三列>第二列>第一列

PyQt5 网格布局拉伸

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn