Pyqt5系列(一)

上传人:公**** 文档编号:564771753 上传时间:2023-06-18 格式:DOC 页数:29 大小:635KB
返回 下载 相关 举报
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,

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

3、PyQt, Pip 安装的方式比较复杂;功能验证:安装之后简单写个测试程序验证一下python view plain copy在 CODE 上查看代码片派生到我的代码片#!/user/bin/python3#-*- coding:utf-8 -*-Creat a simple window_author_ = Tony Zhuimport sysfrom PyQt5.QtWidgets import QWidget, QApplicationif _name_ = _main_:app = QApplication(sys.argv)w = QWidget()w.show()w.setWind

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

5、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)3/29.inputLayout = QHBoxLayout()i

6、nputLayout.addWidget(self.inputLabel)inputLayout.addWidget(self.editLine)buttonLayout = QHBoxLayout()buttonLayout.addWidget(self.printButton)buttonLayout.addWidget(self.clearButton)mainLayout = QVBoxLayout()mainLayout.addLayout(inputLayout)mainLayout.addLayout(buttonLayout)self.setLayout(mainLayout)

7、self.setWindowTitle(FristWindow)self.show()def printText(self):if text = :QMessageBox.information(self, Empty Text,Please enter the letter.)else :QMessageBox.information(self, Print Success,Text: %s % text)def clearText(self):if text = :returnelse :if _name_ = _main_:app = QApplication(sys.argv)ex =

8、 ShowWindow()sys.exit(app.exec_()运行的结果:程序运行后的结果结合代码和运行的结果,分析代码:4/29.Line78 :from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QPushButton, QLineEdit, QVBoxLayout,QMessageBoximport sys导入了程序运行所必须的模块Line10 :class ShowWindow(QWidget):创建一个ShowWindow 类继承 QWidget ,其中 PyQt 中非常重要的通用窗口类,

9、是所有用户界面对象的基类,所有和用户界面相关的控件类都是继承自QWidger 类。Line1214 :def _init_(self):super(ShowWindow,self)._init_()self.initUI()定义了 ShowWindow 类的构造函数 init ,由于 ShowWindow 继承自 QWidgert 类,因此在构造函数中调用父类 QWidget 的构造函数 super.init() 。同时在构造函数中调用自定义的初始化函数 initUI() ,在该函数中初始化 GUI 中所需各个控件。Line1720 :self.inputLabel = QLabel(Inpu

10、t your text)self.editLine = QLineEdit()self.printButton = QPushButton(Print)self.clearButton = QPushButton(Clear)创建成员:一个标签( inputLabel),输入框( editLine),两个按钮( printButton,clearButton)Line2223 :1printButton和 clearButton 点击事件处理的逻辑:点击printButton之后会调用自定义函数printText() 中的处理逻辑;点击 clearButton 之后调用自定义函数 clearT

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

12、out) ,在 inputLayout 中添加已定义的控件 inputLabel 和 editLine 。 QHBoxLayout 让添加的控件水平排布Line2931:buttonLayout = QHBoxLayout()buttonLayout.addWidget(self.printButton)buttonLayout.addWidget(self.clearButton)同上创建一个buttonLayout的水平布局 (QHBoxLayout) ,在 buttonLayout中添加 printButton和 clearButton Line3335 :mainLayout = QVBoxLayout()mainLayout.addLayout(inputLayout)mainLayout.addLayout(buttonLayout)创建一个mainLayout的垂直布局 (QVBoxLayout) ,在 mainLayout中嵌套子布局inputLayout和 buttonLayout 。通过如下的布局图,进一步了解一下该程序中layout 是

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

当前位置:首页 > 办公文档 > 演讲稿/致辞

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