1 #demo_11:网格布局
2 import sys
3 from PyQt5.QtWidgets import QApplication,QWidget,QGridLayout,QPushButton
4
5 class Example(QWidget):
6 def __init__(self):
7 super().__init__()
8 self.initUI()
9 def initUI(self):
10 self.names = ['Cls', 'Bck', '', 'Close',
11 '7', '8', '9', '/',
12 '4', '5', '6', '*',
13 '1', '2', '3', '-',
14 '0', '.', '=', '+']
15 self.options=[(j,i) for j in range(5) for i in range(4)]
16 self.layout=QGridLayout()
17 self.setLayout(self.layout)
18
19 for (name,option) in zip(self.names,self.options):
20 if name=='':continue
21 btn=QPushButton(name)
22 self.layout.addWidget(btn,*option)
23 self.setWindowTitle('Calculator')
24 self.show()
25 if __name__=='__main__':
26 app=QApplication(sys.argv)
27 e=Example()
28 sys.exit(app.exec())
29
30 # names = ['Cls', 'Bck', '', 'Close',
31 # '7', '8', '9', '/',
32 # '4', '5', '6', '*',
33 # '1', '2', '3', '-',
34 # '0', '.', '=', '+']
35 # options = [(i, j) for j in range(5) for i in range(4)]
36 # for position, name in zip(options, names):
37 # print(str(position)+"》》"+str(name))