pyqt5系列(一)

上传人:第*** 文档编号:31777190 上传时间:2018-02-09 格式:DOC 页数:29 大小:664.50KB
返回 下载 相关 举报
pyqt5系列(一)_第1页
第1页 / 共29页
pyqt5系列(一)_第2页
第2页 / 共29页
pyqt5系列(一)_第3页
第3页 / 共29页
pyqt5系列(一)_第4页
第4页 / 共29页
pyqt5系列(一)_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《pyqt5系列(一)》由会员分享,可在线阅读,更多相关《pyqt5系列(一)(29页珍藏版)》请在金锄头文库上搜索。

1、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

2、 之后,选择使用 Pyqt 来进行 GUI 的开发,PyQt 基于GPL 许可开发。PyQt 安装:整体安装的步骤比较简单,首先下载与自己 python 版本和开发环境一致的 PyQt 版本。1、开发环境:python3.35win7 32bit2、官网下载:官网上之后对应的 source package。需要自己编译生成。3、OSDN 下载:OSDN 上罗列了所有 released 的 PyQt 安装程序,根据开发环境下载了 PyQt5-5.2.1-gpl-Py3.3-Qt5.2.0-x32.exe 安装程序。Note:1、下载 PyQt 时注意选择匹配的 Python 版本和系统的位数;2

3、、直接通过 exe 文件安装 PyQt,Pip 安装的方式比较复杂;功能验证:安装之后简单写个测试程序验证一下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 = QWi

4、dget() w.show() w.setWindowTitle(Hello PyQt5) sys.exit(app.exec_() 运行之后会直接显示一个标题为“Hello PyQt5”的空白窗口。二 第一个 PyQt 程序通过下面的一个 PyQt5 简单例子,来简单了解一下关于如何创建 PyQt5 的。具体代码如下:#-*- coding:utf-8 -*-Frist PyQt5 program_author_ = Tony Zhufrom PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QPushBut

5、ton, QLineEdit, QVBoxLayout,QMessageBoximport sysclass 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) sel

6、f.printButton.clicked.connect(self.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.clearButto

7、n)mainLayout = QVBoxLayout()mainLayout.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 :QMess

8、ageBox.information(self, Print Success,Text: %s % text)def clearText(self):text = self.editLine.text()if text = :returnelse :self.editLine.clear()if _name_ = _main_:app = QApplication(sys.argv)ex = ShowWindow()sys.exit(app.exec_() 运行的结果: 程序运行后的结果结合代码和运行的结果,分析代码: Line78:from PyQt5.QtWidgets import QA

9、pplication, QWidget, QLabel, QHBoxLayout, QPushButton, QLineEdit, QVBoxLayout,QMessageBoximport sys导入了程序运行所必须的模块Line10:class ShowWindow(QWidget):创建一个 ShowWindow 类继承 QWidget,其中 PyQt 中非常重要的通用窗口类,是所有用户界面对象的基类,所有和用户界面相关的控件类都是继承自 QWidger 类。Line1214:def _init_(self):super(ShowWindow,self)._init_() self.in

10、itUI()定义了 ShowWindow 类的构造函数 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(Cl

11、ear)创建成员:一个标签(inputLabel ) ,输入框(editLine ) ,两个按钮(printButton ,clearButton )Line2223:self.printButton.clicked.connect(self.printText) self.clearButton.clicked.connect(self.clearText)1printButton 和 clearButton 点击事件处理的逻辑:点击 printButton 之后会调用自定义函数printText()中的处理逻辑;点击 clearButton 之后调用自定义函数 clearText()中的处

12、理逻辑。通过 connect()方法将点击事件和处理逻辑关联起来 这个涉及到 PyQt 信号与槽的概念,信号和槽用于对象之间的通信。当指定事件发生,一个事件信号会被发射。槽可以被任何 Python 脚本调用。当和槽连接的信号被发射时,槽会被调用。有关信号与槽的概念,在后续的文章中会专门讲解。Line2526:inputLayout = QHBoxLayout()inputLayout.addWidget(self.inputLabel)inputLayout.addWidget(self.editLine)创建一个 inputLayout 的水平布局 (QHBoxLayout),在 input

13、Layout 中添加已定义的控件inputLabel 和 editLine。QHBoxLayout 让添加的控件水平排布Line2931:buttonLayout = QHBoxLayout()buttonLayout.addWidget(self.printButton)buttonLayout.addWidget(self.clearButton)同上创建一个 buttonLayout 的水平布局(QHBoxLayout),在 buttonLayout 中添加printButton 和 clearButtonLine3335:mainLayout = QVBoxLayout()mainLa

14、yout.addLayout(inputLayout)mainLayout.addLayout(buttonLayout)创建一个 mainLayout 的垂直布局 (QVBoxLayout),在 mainLayout 中嵌套子布局inputLayout 和 buttonLayout。通过如下的布局图,进一步了解一下该程序中 layout 是如何布局的。有 layout 的概念,在后续的文章中会专门讲解。Line37:self.setLayout(mainLayout)通过将 setLayout()方法,将 mainLayout 设置为窗口 layout。Line38:self.setWind

15、owTitle(FristWindow)通过 setWindowTitle()方法设置窗口的标题Line39:self.show()通过 show()方法将窗口显示到屏幕上Line4048:def printText(self):text = self.editLine.text()if text = :QMessageBox.information(self, Empty Text,Please enter the letter.)else :QMessageBox.information(self, Print Success,Text: %s % text)定义了处理 printButt

16、on 点击信号的槽函数,当 editLine 输入框中的文本为空的时候弹出消息框(QMessageBox),提示“Please enter the letter.”;当 editLine 输入框中的文本不为空时候弹出消息框,显示 editLine 中的文本。触发槽函数 printText()之后,当 editLine 输入框内容为空时界面显示如下:Line4955:def clearText(self):text = self.editLine.text()if text = : returnelse :self.editLine.clear()定义了处理 clearButton 点击信号的槽函数,清空 editL

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 工业设计

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号