|
楼主 |
发表于 2014-7-14 00:38:37
|
显示全部楼层
PyQt\每日一练0703 网格布局
本帖最后由 ~风介~ 于 2014-8-17 12:46 编辑
网格布局
最通用的布局类别是网格布局 (QGridLayout) 。 该布局方式将窗口空间划分为许多行和
列。要创建该布局方式,我们需要使用 QGridLayout 类。
代码:
- #!/usr/bin/python
- # gridlayout.py
- import sys
- from PyQt5 import QtWidgets
- class 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 = QtWidgets.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 = QtWidgets.QPushButton(i)
- if j == 2:
- #pass
- grid.addWidget(QtWidgets.QLabel(''), 0, 2)
- else:
- grid.addWidget(button, pos[j][0], pos[j][1])
- #print(j,' ',pos[j][0] , '===' ,pos[j][1])
- j = j + 1
- self.setLayout(grid)
- app = QtWidgets.QApplication(sys.argv)
- qb = GridLayout()
- qb.show()
- sys.exit(app.exec_())
- '''在这个示例中, 我们创建一组按网格布局的按钮。 为了填补 Bck 和 Close 按钮之间的空
- 白,我们使用 QLabel 部件。
- grid = QtGui.QGridLayout()
- 该语句创建了一个网格布局。
- if j == 2:
- grid.addWidget(QtGui.QLabel(''), 0, 2)
- else:
- grid.addWidget(button, pos[j][0], pos[j][1])
- 使用 addWidget()方法,我们将部件加入到网格布局中。addWidget()方法的参数依次为
- 要加入到局部的部件,行号和列号。'''
复制代码
截图:
|
|