PyQt5 布局

 1 import sys
 2 
 3 from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QGridLayout
 4 
 5 
 6 class Example(QWidget):
 7     def __init__(self):
 8         super().__init__()
 9         self.initUI()
10 
11     def initUI(self):
12         vlayout = QVBoxLayout()  # 垂直布局
13 
14         hlayout = QHBoxLayout()  # 水平布局
15 
16         for i in range(10):  # 十个标签
17             lab1 = QLabel("label" + str(i), self)
18             # lab1.move(15, 10 + (i * 30))
19             hlayout.addWidget(lab1)
20         vlayout.addLayout(hlayout)  # 水平布局添加到垂直布局中
21 
22         for i in range(10):  # 十个标签
23             lab1 = QLabel("label" + str(i), self)
24             # lab1.move(15, 10 + (i * 30))
25             vlayout.addWidget(lab1)
26 
27         self.setLayout(vlayout)
28 
29         self.setGeometry(100, 100, 300, 600)
30         self.setWindowTitle("定位测试")
31         self.show()
32 
33 
34 class Test(QWidget):
35     def __init__(self):
36         super().__init__()
37         self.initUI()
38 
39     def initUI(self):
40         okButton = QPushButton("确定")
41         cancelButton = QPushButton("取消")
42         hbox = QHBoxLayout()
43         hbox.addStretch(1)
44         hbox.addWidget(okButton)
45         hbox.addWidget(cancelButton)
46 
47         vbox = QVBoxLayout()
48         vbox.addStretch(1)
49         vbox.addLayout(hbox)
50 
51         self.setLayout(vbox)
52         self.setGeometry(100, 100, 500, 500)
53         self.show()
54 
55 
56 class gridLayout(QWidget):
57     def __init__(self):
58         super().__init__()
59         self.initUI()
60 
61     def initUI(self):
62         grid = QGridLayout()
63         self.setLayout(grid)
64         names = ['Cls', 'Bck', '', "Close",
65                  "7", "8", "9", "/",
66                  "4", "5", "6", "*",
67                  "1", "2", "3", "-",
68                  "0", ".", "=", "+"]
69         positions = [(i, j) for i in range(5) for j in range(4)]
70         for position, name in zip(positions, names):
71             if name == "":
72                 continue
73             button = QPushButton(name)
74             grid.addWidget(button, *position)
75         label = QLabel("计算器计算器计算器计算器计算器计算器计算器计算器计算器计算器计算器计算器计算器计算器");
76         grid.addWidget(label, 5, 0, 1, 4)  # 夸四列
77         lab1 = QLabel("计算器1")
78         grid.setSpacing(10)
79         grid.addWidget(lab1, 9, 0, 4, 1)  # 夸四行
80         self.move(300, 150)
81         self.setWindowTitle("计算器")
82         self.show()
83 
84 
85 if __name__ == '__main__':
86     app = QApplication(sys.argv)
87     # ex = Example()
88     # t = Test()
89     c = gridLayout()
90     sys.exit(app.exec_())

 

posted on 2018-04-09 10:58  lanyue52011  阅读(278)  评论(0编辑  收藏  举报

导航