最强大的GUI库 — PyQt4
Authors
  ~ 机械唯物主义
<[linjunhalida@gmai.com](mailto:linjunhalida@gmai.com)\>
一个简单的实例

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  | #!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Caculator(QDialog):
    def __init__(self):
        super(Caculator, self).__init__()
        #widgets
        self.leInput = QLineEdit()
        self.lwResult = QListWidget()
        #layouts
        l = QVBoxLayout(self)
        for w in self.leInput, self.lwResult:
            l.addWidget(w)
        #events
        self.leInput.returnPressed.connect(self.caculate)
    def caculate(self):
        data = unicode(self.leInput.text())
        if not data: return
        self.leInput.clear()
        try:
            result = unicode(eval(data))
        except Exception as e:
            result = unicode(e)
        self.lwResult.addItem(result)
def main():
    app = QApplication([])
    Caculator().exec_()
if __name__=="__main__":
    main()
  | 
 
类层级
QObject
   |----- QWidget
             |----- QDialog
             |----- QLineEdit
             |----- QListWidget
layout

layout
1
2
3
  | l = QVBoxLayout(self)
for w in self.leInput, self.lwResult:
    l.addWidget(w)
  | 
 
Signal and Slot in Qt

Qt和PyQt 事件机制区别
1
  | this->connect(leInput, SINGAL(returnPressed()), this, caculate))
  | 
 
1
  | self.leInput.returnPressed.connect(self.caculate)
  | 
 
一个复杂的实例: 扫雷
- 花费时间: 2个晚上, 基础:2.5小时, 一点点提升:1小时
 
- 扫雷下载: pyqtmine
 

UI designer

UI with Code
1
2
3
4
5
  | form, base = uic.loadUiType("score.ui")
class ScoreDlg(QDialog, form):
    def __init__(self):
        super(ScoreDlg, self).__init__()
        self.setupUi(self)
  | 
 
Event
1
2
3
4
5
6
7
8
  | def mouseReleaseEvent(self, event):
    if event.button() == Qt.LeftButton:
        ...
def paintEvent(self, event=None):
    p = QPainter(self)
    ...
p.drawLine(mx+i*sx, my, mx+i*sx, my+y*sy)
  | 
 
其他强大特性
- 足够多和好用的控件/自定控件/整合到designer中
 
- webkit/script支持
 
- 强大/方便/快速的绘图控件
 
- 富文本/文本解析
 
- 多国语言支持
 
- 其他第三方控件支持: pyqwt
 
发布
pyinstaller!
资源