PyQt5:网格布局(13)

来源:互联网 发布:父母干涉爱情知乎 编辑:程序博客网 时间:2024/05/16 12:07

学习《PyQt4入门指南 PDF中文版.pdf 》笔记

最通用的布局类别是网格布局(QGridLayout)。该布局方式将窗口空间划分为许多行和列。要创建该布局方式,我们需要实用QGridLayout类。

#!/usr/bin/python# gridlayout.pyfrom PyQt5.QtWidgets import QApplication, QPushButton, QLabel, QGridLayoutfrom PyQt5 import QtWidgetsclass GridLayout(QtWidgets.QWidget):    def __init__(self, parent= None):        QtWidgets.QWidget.__init__(self)          self.setWindowTitle('grid layout')                names = ['Cls', 'Bck','', 'Close', '7', '8', '9', '/',         '4', '5', '6', '*', '1', '2',  '3', '-', '0', '.', '=', '+']                grid = QGridLayout()        j = 0        pos = [(0, 0), (0, 1), (0, 2), (0, 3),                     (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),]                for i in names:            button = QPushButton(i)            if j == 2:                grid.addWidget(QLabel(''),  0,  2)            else:                grid.addWidget(button, pos[j][0],  pos[j][1])            j = j + 1;                self.setLayout(grid)        self.resize(300,  150)if __name__ == "__main__":    import sys    app = QApplication(sys.argv)    qb = GridLayout()    qb.show()    sys.exit(app.exec_())

在这个示例中,我们创建一组按照网格布局的按钮。为了填补Bck和Close按钮之间的空白,我们使用QLabel部件。

         grid= QGridLayout()

         该语句创建了一个网格布局。

         if  j == 2:

                 grid.addWidget(QLabel(''), 0,  2)

         else:

                 grid.addWidget(button, pos[j][0], pos[j][1])

         使用addWidget()方法,我们将部件加入到网格布局中。addWidget()方法的参数依次为要加入到局部的部件,行号和列号。


0 0
原创粉丝点击