最强大的GUI库 — PyQt4
Authors ~ 机械唯物主义 <linjunhalida@gmai.com>
一个简单的实例
- 计算器
- 10分钟
代码
#!/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
-
层级:
QDialog (QVBoxLayout) |----- QLineEdit |----- QListWidget -
代码:
l = QVBoxLayout(self)
for w in self.leInput, self.lwResult:
l.addWidget(w)
Signal and Slot in Qt
Qt和PyQt 事件机制区别
- Qt:
this->connect(leInput, SINGAL(returnPressed()), this, caculate))
- PyQt:
self.leInput.returnPressed.connect(self.caculate)
一个复杂的实例: 扫雷
- 花费时间: 2个晚上, 基础:2.5小时, 一点点提升:1小时
- 扫雷下载: pyqtmine
UI designer
UI with Code
form, base = uic.loadUiType("score.ui")
class ScoreDlg(QDialog, form):
def __init__(self):
super(ScoreDlg, self).__init__()
self.setupUi(self)
Event
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
评论
评论通过 GitHub 账号发布,需要先使用 GitHub 登录。