PyQt5最全76 布局之QGridLayout实现计算机UI

PyQt5最全76 布局之QGridLayout实现计算机UI

import sys
from PyQt5.QtWidgets import *


class GridLayoutCalc(QWidget):
    """
    栅格布局:QGridLayout()
    实现计算机UI

    """

    def __init__(self):
        super(GridLayoutCalc, self).__init__()
        self.setWindowTitle('栅格布局')
        self.resize(400, 300)

        grid = QGridLayout()
        self.setLayout(grid)

        # 利用循环增加按钮
        names = ['Cls', 'Back', '', 'Close',
                 '7', '8', '9', '/',
                 '4', '5', '6', '*',
                 '1', '2', '3', '-',
                 '0', '.', '=', '+']
        positions = [(i, j) for i in range(5) for j in range(4)]
        print(positions)

        # zip 可以将positions和names合在一起
        for position, name in zip(positions, names):
            if name == '':
                continue
            button = QPushButton(name)
            # position 是一个元组    前面加*可以打开 eg: *(1, 2) = 1 2
            grid.addWidget(button, *position)


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

PyQt5最全76 布局之QGridLayout实现计算机UI

上一篇:76. 最小覆盖子串


下一篇:Leetcode - 76. 最小覆盖子串