PyQt 入门介绍

Posted by Kerwen Blog on April 4, 2016

介绍

PyQt是一个创建GUI应用程序的工具包。它是Python编程语言和Qt库的成功融合。它有超过300类,将近6000个函数和方法。它是一个多平台的工具包,可以运行在所有主要操作系统上,包括UNIX,Windows和Mac。 PyQt采用双许可证,开发人员可以选择GPL和商业许可。在此之前,GPL的版本只能用在Unix上,从PyQt的版本4开始,GPL许可证可用于所有支持的平台。

PyQt分成以下几个模块:
QtCore模块包含核心的非GUI功能。该模块用于时间、文件和目录、各种数据类型、流、网址、MIME类型、线程或进程。
QtGui模块包含图形组件和相关的类,例如按钮、窗体、状态栏、工具栏、滚动条、位图、颜色、字体等。
QtNetwork模块包含了网络编程的类,这些类允许编写TCP/IP和UDP的客户端和服务器,他们使网络编程更简单,更轻便。
QtXml包含使用XML文件的类,这个模块提供了SAX和DOM API的实现。
QtSvg模块提供显示的SVG文件的类。可缩放矢量图形(SVG)是一种用于描述二维图形和图形应用程序的XML语言。
QtOpenGL模块使用OpenGL库渲染3D和2D图形,该模块能够无缝集成Qt的GUI库和OpenGL库。
QtSql模块提供用于数据库的类。

下载

PyQt4 官方下载地址: https://riverbankcomputing.com/software/pyqt/download
注意: PyQt4 可能不支持最新的Python,目前Python已经更新到Python3.52,但PyQt4只支持到Python3.4. PyQt在安装时会自动寻找Python的安装目录, 安装完成后放在 Lib\site-packages\PyQt4 目录下。

使用

代码实现

PyQt可以写代码添加各种控件

标签:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import sys
from PyQt4 import QtCore, QtGui
class Window( QtGui.QMainWindow ):
	def __init__( self ):
		super( Window, self ).__init__()
		self.setWindowTitle( "hello" )
		self.resize( 200, 300 )

		#添加标签

		label = QtGui.QLabel( "label" )
		label.setAlignment( QtCore.Qt.AlignCenter )
		self.setCentralWidget( label )
app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()

效果图:
Label

按钮:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QWidget ):
	def __init__( self ):
		super( Window, self ).__init__()
		self.setWindowTitle( "hello" )
		self.resize( 500, 500 )
		gridlayout = QtGui.QGridLayout()
		 
		button1 = QtGui.QPushButton( "button1" )
		gridlayout.addWidget( button1, 0, 0, 1, 3 )
		 
		button2 = QtGui.QPushButton( "button2" )
		button2.setFlat( True )
		gridlayout.addWidget( button2, 1, 1, 1, 3 )
		self.setLayout( gridlayout )
app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()

单行文本和多行文本:

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
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QWidget ):
	def __init__( self ):
		super( Window, self ).__init__()
		self.setWindowTitle( "hello" )
		self.resize( 500, 500 )
		 
		gridlayout = QtGui.QGridLayout()
		 
		str = "hello"  #这里中文乱码,纠结
		label = QtGui.QLabel( str )
		label.setAlignment( QtCore.Qt.AlignCenter )
		 
		textFile = QtGui.QLineEdit()
		gridlayout.addWidget( label, 0, 0 )
		gridlayout.addWidget( textFile )
		 
		passwordFile = QtGui.QLineEdit()
		passwordFile.setEchoMode( QtGui.QLineEdit.Password )
		gridlayout.addWidget( passwordFile )
		 
		textArea = QtGui.QTextEdit()
		textArea.setText( "asdasda" )
		gridlayout.addWidget( textArea )
		 
		 
		self.setLayout( gridlayout )
app = QtGui.QApplication( sys.argv )
window = Window()
window.show()
app.exec_()

单选和复选框

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
38
39
40
41
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QWidget ):
	def __init__( self ):
		super( Window, self ).__init__()
		self.setWindowTitle( "hello" )
		self.resize( 500, 500 )
		hboxlayout = QtGui.QHBoxLayout()
		 
		self.radio1 = QtGui.QRadioButton( "radio1" )
		self.radio2 = QtGui.QRadioButton( "radio2" )
		self.radio3 = QtGui.QRadioButton( "radio3" )
		self.radio1.setChecked( True )
		 
		hboxlayout.addWidget( self.radio1 )
		hboxlayout.addWidget( self.radio2 )
		hboxlayout.addWidget( self.radio3 )
		 
		checkbox1 = QtGui.QCheckBox( "checkbox1" )
		checkbox2 = QtGui.QCheckBox( "checkbox2" )
		checkbox3 = QtGui.QCheckBox( "checkbox3" )
		checkbox1.setChecked( True )
		 
		hboxlayout.addWidget( checkbox1 )
		hboxlayout.addWidget( checkbox2 )
		hboxlayout.addWidget( checkbox3 )
 
		self.button = QtGui.QPushButton( "Ok" )
		hboxlayout.addWidget( self.button )
		 
		self.connect( self.button, QtCore.SIGNAL( 'clicked()' ), self.OnButton )
		 
		self.setLayout( hboxlayout )
	def OnButton( self ):
		if self.radio2.isChecked():
			self.radio2.setText( "haha" )
		 
app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()

菜单

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
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QMainWindow ):
	def __init__( self ):
		super( Window, self ).__init__()
		self.setWindowTitle( "hello" )
		self.resize( 500, 500 )
		 
		 
		 
		menubar = self.menuBar()
		self.file = menubar.addMenu( '&File' )
		open = self.file.addAction( 'Open' )
		self.connect( open, QtCore.SIGNAL( 'triggered()' ), self.OnOpen )
		 
		save = self.file.addAction( 'Save' )
		self.connect( save, QtCore.SIGNAL( 'triggered()' ), self.OnSave )
		self.file.addSeparator()
		close = self.file.addAction( "Close" )
		self.connect( close, QtCore.SIGNAL( 'triggered()' ), self.OnClose )
		 
		self.label = QtGui.QLabel( "this is a  google test" )
		self.label.setAlignment( QtCore.Qt.AlignCenter )
		self.setCentralWidget( self.label )
		 
	def OnOpen( self ):
		self.label.setText( "open" )
	def OnSave( self ):
		self.label.setText( "save" )
	def OnClose( self ):
		self.close()
	def contextMenuEvent( self, event ):
	   self.file.exec_( event.globalPos() )
app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()

designer

PyQt还可以通过designer来实现UI界面的设计

designer.exe 位置:Python\Lib\site-packages\PyQt4
可以通过鼠标拖拽的方式实现界面设计。
界面设计完成后点击保存,会生成一个.ui格式的文件
如果想让它按照我们设计的界面自动生成该界面的.py代码, 可以选择菜单 窗体 -> 查看代码.
注意:如果你的Python安装路径里面有空格,可能会造成代码生成失败。
这时可以通过命令行的方式来实现。
先找到.ui文件的目录 Python\Lib\site-packages\PyQt4\uic 该目录下有一个pyuic.py的程序
通过命令行调用:

1
pyuic -o xxx.py yyy.ui 

yyy.ui为你的UI设计文件,指定要生成的py文件名。
如果想让该py文件直接能执行,还需要做些修改。 打开py文件
找到类声明的地方

1
2
3
class Ui_MainWindow(object):
	def setupUi(self, MainWindow):
		MainWindow.setObjectName(_fromUtf8("MainWindow"))

该类继承自object类,这里修改成QtGui.QMainWindow, 然后添加一个__init__方法。跟手动写的代码很相似。

1
2
3
4
5
6
7
8
class Ui_MainWindow(QtGui.QMainWindow):
	def __init__(self):
		super(Ui_MainWindow,self).__init__()
		self.setupUi(self)
		self.retranslateUi(self)
		
	def setupUi(self, MainWindow):
		MainWindow.setObjectName(_fromUtf8("MainWindow"))

最后加上Main调用就可以看见界面了。

1
2
3
4
app = QtGui.QApplication( sys.argv )
win = Ui_MainWindow()
win.show()
app.exec_()

参考资料:
http://www.cnblogs.com/rollenholt/archive/2011/11/16/2251904.html
http://www.52ij.com/jishu/426.html
http://www.52ij.com/jishu/432.html