Pyqt5系列.docx
《Pyqt5系列.docx》由会员分享,可在线阅读,更多相关《Pyqt5系列.docx(35页珍藏版)》请在三一办公上搜索。
1、Pyqt5系列Pyqt5系列 一 Pyqt5的安装 由于在实际的工作中需要使用到python来进行GUI的开发,基于python的GUI开发,只是去考虑具体使用依赖那个GUI库来进行开发。 GUI库的选择: 1、TKinter,python配备的标准gui库,但是功能比较弱,似乎只适合开发非常简单的界面。 2、Wxpython,目前官方网站还没有release针对python3.0以上版本的库,虽然在国外的博客上有针对python3的版本。 3、Pyqt,Pyqt基于QT,在GUI开发上可以参考QT的开发 。 对比了Tkinter,Wxpython,Pyqt之后,选择使用Pyqt来进行GUI的
2、开发,PyQt基于GPL许可开发。 PyQt安装: 整体安装的步骤比较简单,首先下载与自己python版本和开发环境一致的PyQt版本。 1、开发环境: python3.35 win7 32bit 2、官网下载: 官网上之后对应的source package。需要自己编译生成。 3、OSDN下载: OSDN上罗列了所有released的PyQt安装程序,根据开发环境下载了PyQt5-5.2.1-gpl-Py3.3-Qt5.2.0-x32.exe 安装程序。 Note: 1、下载PyQt时注意选择匹配的Python版本和系统的位数; 2、直接通过exe文件安装PyQt,Pip安装的方式比较复杂;
3、 功能验证: 安装之后简单写个测试程序验证一下 python view plain copy 在CODE上查看代码片派生到我的代码片 #!/user/bin/python3 #-*- coding:utf-8 -*- Creat a simple window _author_ = Tony Zhu import sys from PyQt5.QtWidgets import QWidget, QApplication if _name_ = _main_: app = QApplication(sys.argv) w = QWidget w.show w.setWindowTitle(Hel
4、lo PyQt5) sys.exit(app.exec_) 运行之后会直接显示一个标题为“Hello PyQt5”的空白窗口。 二 第一个PyQt程序 通过下面的一个PyQt5简单例子,来简单了解一下关于如何创建PyQt5的。具体代码如下: #-*- coding:utf-8 -*- Frist PyQt5 program _author_ = Tony Zhu from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QPushButton, QLineEdit, QVBoxLayout,QMessageB
5、ox import sys class ShowWindow(QWidget): def _init_(self): super(ShowWindow,self)._init_ self.initUI def initUI(self): self.inputLabel = QLabel(Input your text) self.editLine = QLineEdit self.printButton = QPushButton(Print) self.clearButton = QPushButton(Clear) self.printButton.clicked.connect(self
6、.printText) self.clearButton.clicked.connect(self.clearText) inputLayout = QHBoxLayout inputLayout.addWidget(self.inputLabel) inputLayout.addWidget(self.editLine) buttonLayout = QHBoxLayout buttonLayout.addWidget(self.printButton) buttonLayout.addWidget(self.clearButton) mainLayout = QVBoxLayout mai
7、nLayout.addLayout(inputLayout) mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) self.setWindowTitle(FristWindow) self.show def printText(self): text = self.editLine.text if text = : QMessageBox.information(self, Empty Text, Please enter the letter.) else : QMessageBox.information(self,
8、Print Success, Text: %s % text) def clearText(self): text = self.editLine.text if text = : return else : self.editLine.clear if _name_ = _main_: app = QApplication(sys.argv) ex = ShowWindow sys.exit(app.exec_) 运行的结果: 程序运行后的结果 结合代码和运行的结果,分析代码: Line78: from PyQt5.QtWidgets import QApplication, QWidget
9、, QLabel, QHBoxLayout, QPushButton, QLineEdit, QVBoxLayout,QMessageBox import sys 导入了程序运行所必须的模块 Line10: class ShowWindow(QWidget): 创建一个ShowWindow类继承QWidget,其中PyQt中非常重要的通用窗口类,是所有用户界面对象的基类,所有和用户界面相关的控件类都是继承自QWidger类。 Line1214: def _init_(self): super(ShowWindow,self)._init_ self.initUI 定义了ShowWindow类的
10、构造函数 init,由于ShowWindow继承自QWidgert类,因此在构造函数中调用父类QWidget的构造函数super.init。 同时在构造函数中调用自定义的初始化函数initUI,在该函数中初始化GUI中所需各个控件。 Line1720: self.inputLabel = QLabel(Input your text) self.editLine = QLineEdit self.printButton = QPushButton(Print) self.clearButton = QPushButton(Clear) 创建成员:一个标签,输入框,两个按钮 Line2223:
11、self.printButton.clicked.connect(self.printText) self.clearButton.clicked.connect(self.clearText) 1 printButton和clearButton点击事件处理的逻辑:点击printButton之后会调用自定义函数printText中的处理逻辑;点击clearButton之后调用自定义函数clearText中的处理逻辑。通过connect方法将点击事件和处理逻辑关联起来 这个涉及到PyQt信号与槽的概念,信号和槽用于对象之间的通信。当指定事件发生,一个事件信号会被发射。槽可以被任何Python脚本
12、调用。当和槽连接的信号被发射时,槽会被调用。有关信号与槽的概念,在后续的文章中会专门讲解。 Line2526: inputLayout = QHBoxLayout inputLayout.addWidget(self.inputLabel) inputLayout.addWidget(self.editLine) 创建一个inputLayout的水平布局(QHBoxLayout),在inputLayout中添加已定义的控件inputLabel和editLine。QHBoxLayout让添加的控件水平排布 Line2931: buttonLayout = QHBoxLayout buttonLa
13、yout.addWidget(self.printButton) buttonLayout.addWidget(self.clearButton) 同上创建一个buttonLayout 的水平布局(QHBoxLayout),在buttonLayout 中添加printButton和clearButton Line3335: mainLayout = QVBoxLayout mainLayout.addLayout(inputLayout) mainLayout.addLayout(buttonLayout) 创建一个mainLayout 的垂直布局(QVBoxLayout),在mainLayo
14、ut 中嵌套子布局inputLayout和buttonLayout。 通过如下的布局图,进一步了解一下该程序中layout是如何布局的。 有layout的概念,在后续的文章中会专门讲解。 Line37: self.setLayout(mainLayout) 通过将setLayout方法,将mainLayout设置为窗口layout。 Line38: self.setWindowTitle(FristWindow) 通过setWindowTitle方法设置窗口的标题 Line39: self.show 通过show方法将窗口显示到屏幕上 Line4048: def printText(self)
15、: text = self.editLine.text if text = : QMessageBox.information(self, Empty Text, Please enter the letter.) else : QMessageBox.information(self, Print Success, Text: %s % text) 定义了处理printButton点击信号的槽函数,当editLine输入框中的文本为空的时候弹出消息框(QMessageBox),提示“Please enter the letter.”;当editLine输入框中的文本不为空时候弹出消息框,显示
16、editLine中的文本。 触发槽函数printText之后,当editLine输入框内容为空时界面显示如下: Line4955: def clearText(self): text = self.editLine.text if text = : return else : self.editLine.clear 定义了处理clearButton点击信号的槽函数,清空editLine输入框中的文本信息。 Line 5760: if _name_ = _main_: app = QApplication(sys.argv) ex = ShowWindow sys.exit(app.exec_)
17、 程序运行的入口函数类似C语言中的main方法。 app = QApplication(sys.argv),每一个pyqt程序必须创建一个application对象,sys.argv是命令行参数,可以通过命令启动的时候传递参数。 ex = ShowWindow,创建ShowWindow对象。 sys.exit(app.exec_),app.exec_ 事件处理开始,进入程序主循环。主循环等待时间,并把事件发送给指定处理的任务中。当调用app.exit或者程序因为各种原因被破坏后,使用sys.exit关闭程序,并释放内存资源。 到此为止,第一个PyQt程序分析结束,可能会存在输入框,布局,等各类
18、控件如何使用,如何确定对应的控件在那个模块中?在后续的文章中会详细进行介绍。 三 基本界面组件之Button 抽象类 QAbstractButton: QAbstractButton作为抽象类,提供button的通用功能,可按按钮和可选择按钮。可选择按钮实现有QRadioButton和QCheckBox;可按按钮实现有QPushButton和QToolButton。 任何一种button可以显示带文本(.setText方法设置文本)和图标(.setIcon设置图标)的标签。 QAbstractButton 提供的状态: 1、isDown 提示button是否按下 2、isChecked提示bu
19、tton是否已经标记 3、isEnable提示button是否可以被用户点击 4、isCheckAble提示button是否为可标记 5、setAutoRepeat设置button是否在用户长按按钮的时候可以自动重复执行。 QAbstractButton 提供的信号: 1、pressed,当鼠标在button上并点击左键的时候 触发信号 2、released,当鼠标左键被释放的时候触发信号 3、clicked,当鼠标首次按下,然后释放,或者快捷键被释放的时候触发信号 4、toggled,当button的标记状态发生改变的时候触发信号 接下来会针对每一种button进行介绍: QPushButt
20、on : class QPushButton(QAbstractButton) | QPushButton(QWidget parent=None) | QPushButton(str, QWidget parent=None) | QPushButton(QIcon, str, QWidget parent=None) 由此可见QPushButton继承自QAbstractButton,是一种command按钮。点击执行一些命令,或者响应一些问题。常见的诸如“确认”,“申请”,“取消”,“关闭”,“是”,“否”等按钮。 Command Button经常通过文本来描述执行的动作。有时候我们也会
21、通过快捷键来执行对应按钮的命令。 通过一个示例对QPushButton来进行说明: #-*- coding:utf-8 -*- PushButton _author_ = Tony Zhu from PyQt5.QtWidgets import QApplication, QWidget, QPushButton from PyQt5.QtGui import QIcon from PyQt5.QtCore import Qt import sys class PushButton(QWidget): def _init_(self): super(PushButton,self)._init
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Pyqt5 系列
链接地址:https://www.31ppt.com/p-3164826.html