其他分享
首页 > 其他分享> > PyQt5最全76 布局之QGridLayout实现计算机UI

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_())

在这里插入图片描述

标签:__,name,positions,self,PyQt5,76,QGridLayout,main
来源: https://blog.csdn.net/m0_63993933/article/details/121962186