最新消息:20210917 已从crifan.com换到crifan.org

【记录】折腾Python的PyQt4模块

Python GUI crifan 6424浏览 0评论

背景

之前已经折腾过一些Python中GUI方面的模块了:

【整理】Python中的图形库

后来又看到别人提起PyQt4,所以,就打算再折腾折腾PyQt4.

PyQt4简介

搜PyQt4,就可以找到:

http://www.riverbankcomputing.co.uk/software/pyqt/download

->

http://wiki.python.org/moin/PyQt

 

简单总结如下:

QT是啥

Qt本身是:

  • Trolltech开发的
  • 后被Nokia收购了,所以现在也叫做Nokia的Qt
  • 一个C++的库(的大集合)

Qt本身有多个版本:

  • Qt2
  • Qt3
  • Qt4
  • Qt5

等等。

 

PyQt是啥

PyQt是

  • Riverbank Computing Limited
  • 为C++框架的,跨平台的Qt的GUI/XML/SQL而开发的
  • Python捆绑包
  • 针对Qt 2和Qt 3的,即,Python版本的Qt2和Qt3

 

PyQt4是啥

PyQt4是,相对于PyQt之外的,专门针对Qt4,Qt5等的,另外一个Python绑定包;

即Python版本的Qt4,Qt5等。

注意:PyQt4,并不包含Qt本身。

 

PyQt4中的详细内容

PyQt4,包含了:

  • 440左右个类库(6000多个函数)
  • 实现了对应的GUI,XML处理,网络通信,SQL数据库,网络浏览等方面的功能:
    • QtCore
    • QtGui
    • QtNetwork
    • QtXml
    • QtSvg
    • QtOpenGL
    • QtSql

 

PyQt4的资料

关于文档方面,找到:

http://wiki.python.org/moin/PyQt4

http://wiki.python.org/moin/PyQt

参考其解释,找到详细的文档:

http://www.riverbankcomputing.com/static/Docs/PyQt4/html/

 

与此相关的东西

  • Nokia官网本身,也针对Python中的QT,弄出一个东西,叫做:PySide

 

谁在用PyQt4

下面,介绍了,一些使用了PyQt的应用:

http://www.diotavelli.net/PyQtWiki/SomeExistingApplications

其中就包括,之前就听说过的:

http://code.google.com/p/spyderlib/

有空也去折腾试试。

 

下载PyQt4

http://www.riverbankcomputing.co.uk/software/pyqt/download

中就可以找到下载地址。

去下载了,和我当前环境:

  • win7 x64
  • python 2.7.3

对应的:

PyQt-Py2.7-x64-gpl-4.9.6-1.exe

下载得到

 

安装PyQt4

再去参考:

http://www.riverbankcomputing.com/static/Docs/PyQt4/html/installation.html

去安装。

不过发现其是针对源码编译安装的教程,对于此处我直接使用二进制版本的不合适。

我此处,还是自己去运行exe,自己看着安装再说。

双击PyQt-Py2.7-x64-gpl-4.9.6-1.exe去安装:

welcome to pyqt

pyqt license

pyqt choose componets

pyqt found python install folder

show details pyqt install

然后就安装完毕了:

complete install pyqt

 

看到:

Mark Summerfield’s book, Rapid GUI Programming with Python and Qt, is an up-to-date guide to GUI application development with Python 2.5, PyQt4 and Qt 4.2/4.3. More information can be found at http://www.qtrac.eu/pyqtbook.html. Mark recommends, incidentally, GUI Bloopers, as appropriate supplementary reading for PyQt programmers.

的意思是,好像还需要另外安装Qt才可以正常在Python中使用Qt的。

不过后面的代码,还是可以直接执行的,貌似不需要另外安装Qt的。

这也验证了,之前的看到的解释是,PyQt4,实现了Qt的基本功能。

所以不需要另外安装Qt了。

 

使用PyQt4

从:

http://www.diotavelli.net/PyQtWiki/Tutorials

找到很多教程。

参考:

http://zetcode.com/tutorials/pyqt4/

去参考:

http://zetcode.com/tutorials/pyqt4/firstprograms/

写上代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【记录】折腾Python的PyQt4模块
【记录】折腾Python的PyQt4模块
Author: Crifan Li Version: 2013-01-04 Contact: admin at crifan dot com """ #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we create a simple window in PyQt4. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys; from PyQt4 import QtGui; def pyqtDemo(): app = QtGui.QApplication(sys.argv); w = QtGui.QWidget(); w.resize(250, 150); w.move(300, 300); w.setWindowTitle('Simple'); w.show(); sys.exit(app.exec_()); if __name__ == "__main__": pyqtDemo();

看看能否运行。

结果是可以的,效果如图:

pyqt gui show

 

然后又试了另外一个代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【记录】折腾Python的PyQt4模块
【记录】折腾Python的PyQt4模块
Author: Crifan Li Version: 2013-01-04 Contact: admin at crifan dot com """ import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10)) self.setToolTip('This is a <b>QWidget</b> widget') btn = QtGui.QPushButton('Button', self) btn.setToolTip('This is a <b>QPushButton</b> widget') btn.resize(btn.sizeHint()) btn.move(50, 50) self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Tooltips') self.show() def pyqtDemoTooltip(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': pyqtDemoTooltip()

效果为:

pyqt show tooltip

 

好了,之后的事情,就是如何使用API,实现你自己的GUI了。

 

总结

PyQt4,有点意思。

转载请注明:在路上 » 【记录】折腾Python的PyQt4模块

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 在Riverbank的下载页面里面说:These binary installers for Windows contain everything needed for PyQt4 development except for Python itself. 而且后面的安装内容里有: PyQt4 Qt (with database support for MySQL, PostgreSQL, SQLite3 and ODBC) Qt Designer Qt Linguist Qt Assistant …… 也就是说用官方提供的二进制安装包安装的话 它已经自动把别的都安装上了
    mathematrix11年前 (2013-08-24)回复
103 queries in 0.189 seconds, using 23.23MB memory