表格布局 QGridLayout
表格布局将空间划分为行和列。我们使用QGridLayout类创建一个网格布局。
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
gird = QGridLayout()
self.setLayout(gird)
names = ['Cls', 'Bck', 'run', 'Close',
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+']
positions = [(i, j) for i in range(5) for j in range(4)]
for position , name in zip(positions, names):
btn = QPushButton(name)
# *position是一个可变参数
gird.addWidget(btn, *position)
self.setGeometry(400,200,300,170)
self.setWindowTitle('Calculator')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())