16
16
## 目录
17
17
[ 1. 初识和使用窗口] ( #1-初识和使用窗口 )
18
18
19
- [ 2. ] ( #2- )
19
+ [ 2. 控件 ] ( #2-控件 )
20
20
21
- [ 3. ] ( #3- )
21
+ [ 3. 布局管理 ] ( #3-布局管理 )
22
22
23
- [ 4. ] ( #4- )
23
+ [ 4. 事件和信号 ] ( #4-事件和信号 )
24
24
25
25
[ 5. 使用 QSS 来美化界面] ( #5-使用-qss-来美化界面 )
26
26
38
38
39
39
+ 简单的窗口
40
40
41
+ ```python
42
+ # 这里引入了PyQt5.QtWidgets模块,这个模块包含了基本的组件。
43
+ import sys
44
+ from PyQt5.QtWidgets import QApplication, QWidget
45
+
46
+ if __name__ == ' __main__' :
47
+ # 每个PyQt5应用都必须创建一个应用对象。
48
+ # sys.argv是一组命令行参数的列表。
49
+ # Python可以在shell里运行,这个参数提供对脚本控制的功能。
50
+ app = QApplication(sys.argv)
51
+
52
+ # QWidge控件是一个用户界面的基本控件,它提供了基本的应用构造器。
53
+ w = QWidget()
54
+ # resize()方法能改变控件的大小,这里的意思是窗口宽250px,高150px。
55
+ w.resize(250 , 150 )
56
+ # move()是修改控件位置的的方法,它把控件放置到屏幕坐标的(300, 300)的位置。
57
+ # 注:屏幕坐标系的原点是屏幕的左上角。
58
+ w.move(300 , 300 )
59
+ # 我们给这个窗口添加了一个标题,标题在标题栏展示。
60
+ w.setWindowTitle(' Simple' )
61
+ # show()能让控件在桌面上显示出来。
62
+ w.show()
63
+
64
+ # 主循环从窗口上接收事件,并把事件传入到派发到应用控件里。
65
+ # 当调用exit()方法或直接销毁主控件时,主循环就会结束。
66
+ # sys.exit()方法能确保主循环安全退出,外部环境能通知主控件怎么结束。
67
+ # exec_()之所以有个下划线,是因为exec是一个Python的关键字。
68
+ sys.exit(app.exec_())
69
+ ```
70
+
71
+ + 我们一般把窗体信息封装成一个类,比如这样:
72
+
73
+ ```python
74
+ import sys
75
+ from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication
76
+
77
+
78
+ class Example (QWidget ):
79
+
80
+ def __init__ (self ):
81
+ super ().__init__ ()
82
+
83
+ self .initUI()
84
+
85
+
86
+ def initUI (self ):
87
+
88
+ self .resize(250 , 150 )
89
+ self .center()
90
+
91
+ self .setWindowTitle(' Center' )
92
+ self .show()
93
+
94
+ # 后面可以再初始化一些控件(控件见下一节)
95
+
96
+
97
+ def center (self ):
98
+
99
+ qr = self .frameGeometry()
100
+ cp = QDesktopWidget().availableGeometry().center()
101
+ qr.moveCenter(cp)
102
+ self .move(qr.topLeft())
103
+
104
+
105
+ if __name__ == ' __main__' :
106
+
107
+ app = QApplication(sys.argv)
108
+ ex = Example()
109
+ sys.exit(app.exec_())
110
+ ```
111
+
112
+ > center() 方法是将窗体居中于屏幕。
113
+
114
+ < div align= " right" >
115
+ < a href= " #目录" > 返回目录⬆< / a>
116
+ < / div>
117
+
118
+ # # 2. 控件
119
+
120
+ + 标签类:`QLabel`
121
+
122
+ + 文本框类:`QLineEdit` 、`QTextEdit`
123
+
124
+ + 按钮类:`QPushButton` 、`QRadioButton` 、`QCheckBox`
125
+
126
+ + 下拉列表框:`QComboBox`
127
+
128
+ + 计数器:`QSpinBox`
129
+
130
+ + 滑动条:`QSlider`
131
+
132
+ + 窗口绘图类:`QPainter` 、`QPen` 、`QBrush` 、`QPixmap`
133
+
134
+ + 拖曳与剪贴板:`DragEnterEvent` 、`DropEvent` 、`QClipboard`
135
+
136
+ + 日历控件:`QCalendar`
137
+
138
+ + 日期时间控件:`QDateTimeEdit`
139
+
140
+ + 菜单栏:`QMenuBar`
141
+
142
+ + 工具栏:`QToolBar`
143
+
144
+ + 状态栏:`QStatusBar`
145
+
146
+ + 消息弹出式对话框:`QMessageBox`
147
+
148
+ + 输入对话框:`QInputDialog`
149
+
150
+ + 字体选择对话框:`QFontDialog`
151
+
152
+ + 打开保存文件对话框:`QFileDialog`
153
+
154
+ < div align= " right" >
155
+ < a href= " #目录" > 返回目录⬆< / a>
156
+ < / div>
157
+
158
+ # # 3. 布局管理
159
+
160
+ + 盒布局(强适应性)
161
+
162
+ ```python
163
+ import sys
164
+ from PyQt5.QtWidgets import (QWidget, QPushButton,
165
+ QHBoxLayout, QVBoxLayout, QApplication)
166
+
167
+
168
+ class Example (QWidget ):
169
+
170
+ def __init__ (self ):
171
+ super ().__init__ ()
172
+
173
+ self .initUI()
174
+
175
+
176
+ def initUI (self ):
177
+
178
+ okButton = QPushButton(" OK" )
179
+ cancelButton = QPushButton(" Cancel" )
180
+
181
+ hbox = QHBoxLayout()
182
+ hbox.addStretch(1 )
183
+ hbox.addWidget(okButton)
184
+ hbox.addWidget(cancelButton)
185
+
186
+ vbox = QVBoxLayout()
187
+ vbox.addStretch(1 )
188
+ vbox.addLayout(hbox)
189
+
190
+ self .setLayout(vbox)
191
+
192
+ self .setGeometry(300 , 300 , 300 , 150 )
193
+ self .setWindowTitle(' Buttons' )
194
+ self .show()
195
+
196
+
197
+ if __name__ == ' __main__' :
198
+
199
+ app = QApplication(sys.argv)
200
+ ex = Example()
201
+ sys.exit(app.exec_())
202
+ ```
203
+
204
+ + 栅格布局(最常用)
205
+
206
+ ```python
207
+ import sys
208
+ from PyQt5.QtWidgets import (QWidget, QGridLayout,
209
+ QPushButton, QApplication)
210
+
211
+
212
+ class Example (QWidget ):
213
+
214
+ def __init__ (self ):
215
+ super ().__init__ ()
216
+
217
+ self .initUI()
218
+
219
+
220
+ def initUI (self ):
221
+
222
+ grid = QGridLayout()
223
+ self .setLayout(grid)
224
+
225
+ names = [' Cls' , ' Bck' , ' ' , ' Close' ,
226
+ ' 7' , ' 8' , ' 9' , ' /' ,
227
+ ' 4' , ' 5' , ' 6' , ' *' ,
228
+ ' 1' , ' 2' , ' 3' , ' -' ,
229
+ ' 0' , ' .' , ' =' , ' +' ]
230
+
231
+ positions = [(i,j) for i in range (5 ) for j in range (4 )]
232
+
233
+ for position, name in zip (positions, names):
234
+
235
+ if name == ' ' :
236
+ continue
237
+ button = QPushButton(name)
238
+ grid.addWidget(button, * position)
239
+
240
+ self .move(300 , 150 )
241
+ self .setWindowTitle(' Calculator' )
242
+ self .show()
243
+
244
+
245
+ if __name__ == ' __main__' :
246
+
247
+ app = QApplication(sys.argv)
248
+ ex = Example()
249
+ sys.exit(app.exec_())
250
+ ```
251
+
252
+ < div align= " right" >
253
+ < a href= " #目录" > 返回目录⬆< / a>
254
+ < / div>
255
+
256
+ # # 4. 事件和信号
257
+
41
258
```python
42
- # 这里引入了PyQt5.QtWidgets模块,这个模块包含了基本的组件。
43
259
import sys
44
- from PyQt5.QtWidgets import QApplication, QWidget
260
+ from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
261
+
262
+
263
+ class Example (QMainWindow ):
264
+
265
+ def __init__ (self ):
266
+ super ().__init__ ()
267
+
268
+ self .initUI()
269
+
270
+
271
+ def initUI (self ):
272
+
273
+ btn1 = QPushButton(" Button 1" , self )
274
+ btn1.move(30 , 50 )
275
+
276
+ btn2 = QPushButton(" Button 2" , self )
277
+ btn2.move(150 , 50 )
278
+
279
+ btn1.clicked.connect(self .buttonClicked)
280
+ btn2.clicked.connect(self .buttonClicked)
281
+
282
+ self .statusBar()
283
+
284
+ self .setGeometry(300 , 300 , 290 , 150 )
285
+ self .setWindowTitle(' Event sender' )
286
+ self .show()
287
+
288
+
289
+ def buttonClicked (self ):
290
+
291
+ sender = self .sender()
292
+ self .statusBar().showMessage(sender.text() + ' was pressed' )
293
+
45
294
46
295
if __name__ == ' __main__' :
47
- # 每个PyQt5应用都必须创建一个应用对象。
48
- # sys.argv是一组命令行参数的列表。
49
- # Python可以在shell里运行,这个参数提供对脚本控制的功能。
50
- app = QApplication(sys.argv)
51
296
52
- # QWidge控件是一个用户界面的基本控件,它提供了基本的应用构造器。
53
- w = QWidget()
54
- # resize()方法能改变控件的大小,这里的意思是窗口宽250px,高150px。
55
- w.resize(250 , 150 )
56
- # move()是修改控件位置的的方法,它把控件放置到屏幕坐标的(300, 300)的位置。
57
- # 注:屏幕坐标系的原点是屏幕的左上角。
58
- w.move(300 , 300 )
59
- # 我们给这个窗口添加了一个标题,标题在标题栏展示。
60
- w.setWindowTitle(' Simple' )
61
- # show()能让控件在桌面上显示出来。
62
- w.show()
63
-
64
- # 主循环从窗口上接收事件,并把事件传入到派发到应用控件里。
65
- # 当调用exit()方法或直接销毁主控件时,主循环就会结束。
66
- # sys.exit()方法能确保主循环安全退出,外部环境能通知主控件怎么结束。
67
- # exec_()之所以有个下划线,是因为exec是一个Python的关键字。
297
+ app = QApplication(sys.argv)
298
+ ex = Example()
68
299
sys.exit(app.exec_())
69
300
```
70
301
@@ -73,10 +304,36 @@ if __name__ == '__main__':
73
304
</div >
74
305
75
306
## 5. 使用 QSS 来美化界面
76
- https://blog.csdn.net/kuangshp128/article/details/87089446
307
+
308
+ > 参考文章:[ pyqt5中样式的介绍] ( https://blog.csdn.net/kuangshp128/article/details/87089446 )
77
309
78
310
<div align =" right " >
79
311
<a href="#目录">返回目录⬆</a>
80
312
</div >
81
313
82
314
## 6. 将 pyqt5 程序打包为 exe 可执行文件
315
+
316
+ + 使用的打包工具是 pyinstaller
317
+
318
+ ``` python
319
+ pip install pyinstaller
320
+ ```
321
+
322
+ + 执行指令
323
+
324
+ ```python
325
+ pyinstaller - Fw - i favicon xxx.py
326
+ ```
327
+
328
+ > - F:表示生成单个可执行文件;- w:表示去掉控制台窗口;- i:表示增添 icon 图标
329
+
330
+ < div align= " right" >
331
+ < a href= " #目录" > 返回目录⬆< / a>
332
+ < / div>
333
+
334
+ -- -
335
+
336
+ < br>< br>< br>
337
+ < div align= " right" >
338
+ < a href= " ../step4-Algorithm" > Python 数据结构与算法➡< / a>
339
+ < / div>
0 commit comments