Python GUI工具集包括Tkinter、wxPython、PyGTK、PyQt、PySide五种;由于早年学习过Qt,PyQt GUI界面可以通过Qt Designer设计,故选择PyQt;PyQt是Python编程语言和Qt库的成功融合。PyQt向Python程序员提供了使用完整Qt应用程序接口的方法。您几乎可以用Python做任何C++能做的事。既然是应用程序接口,用C++或是Python都是一样的。对我来说作为一种编程语言,python相对于c++的优点是在编程效率上。您可以看到标准的Qt例子移植到PyQt后的代码。它们具有相同的功能,使用相同的应用程序接口,但Python版的代码只有原来的50-60%,而且它们更容易阅读。
PyQt开发工具下载地址,安装包中自带Qt Designer
https://riverbankcomputing.com/software/pyqt/download
点击打开链接
python扩展名
1. *.py
py就是最基本的源码扩展名。windows下直接双击运行会调用python.exe执行;
2. *.pyw
pyw是另一种源码扩展名,跟py唯一的区别是在windows下双击pyw扩展名的源码会调用pythonw.exe执行源码,这种执行方式不
会有命令行窗口;主要用于GUI程序发布时不需要看到控制台信息的情况;
3. *.pyc
在执行python代码时经常会看到同目录下自动生成同名的pyc文件。这是python源码编译后的字节码,一般会在代码执行时自动
生成你代码中引用的py文件的pyc文件。这个文件可以直接执行,用文本编辑器打开也看不到源码;
4. *.pyo
pyo是跟pyc类似的优化编码后的文件。
开发纯图形界面程序的时候,推荐暂时把 .pyw 改成 .py,以便运行时能调出控制台窗口,看到所有错误信息,方便除虫Debug。
1. 第一个PyQt程序---hello kitty
__author__ = '[email protected]'
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
btn = QtGui.QPushButton("Hello Kitty!")
btn.show()
sys.exit(app.exec_())
保存为firt_pyqt.py,直接双击或者命令行下运行
2. 将Qt Designer的UI文件转化为py文件
Dialog
0
0
400
300
Dialog
130
130
75
23
hello world
C:\Python27\Lib\site-packages\PyQt4\uic\pyuic.py -o c:\first_qt.py c:\first_pyqt.ui
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'c:\first_pyqt.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 300)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(130, 130, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton.setText(_translate("Dialog", "hello world", None))
添加头文件和main函数
import sys
from PyQt4 import QtCore, QtGui
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWindow = QtGui.QDialog()
myWindow = Ui_Dialog()
myWindow.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'c:\first_pyqt.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 300)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(130, 130, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton.setText(_translate("Dialog", "hello world", None))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWindow = QtGui.QDialog()
myWindow = Ui_Dialog()
myWindow.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
__author__ = '[email protected]'
import sys
from PyQt4 import QtCore, QtGui, uic
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mWindow = uic.loadUi('first_pyqt.ui')
mWindow.show()
sys.exit(app.exec_())
4. PyQt中使用信号与槽
__author__ = '[email protected]'
import sys
from PyQt4 import QtCore, QtGui
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
btn = QtGui.QPushButton("Hello Kitty!")
btn.show()
app.connect(btn, QtCore.SIGNAL("clicked()"), app, QtCore.SLOT("quit()"))
app.exec_()