ImageVerifierCode 换一换
格式:DOCX , 页数:29 ,大小:760.44KB ,
资源ID:11799221      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11799221.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Pyqt5系列.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Pyqt5系列.docx

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,Wxpyt

2、hon,Pyqt 之后,选择使用 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、下载 Py

3、Qt 时注意选择匹配的 Python 版本和系统的位数;2、直接通过exe文件安装PyQt, Pip安装的方式比较复杂;功能验证:安装之后简单写个测试程序验证一下python view plain copy 在 CODE 上查看代码片派生到我的代码片 #!/user/bin/python3#-*- coding:utf-8 -*-IIIIICreat a simple window _author_ = Tony Zhuimport sysfrom PyQt5.QtWidgets import QWidget, QApplicationif _name_ = _main_:app = QApp

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

5、el, QHBoxLayout, QPushButton,QLin eEdit, QVBoxLayout,QMessageBoximport sys class ShowWi ndow(QWidget):def _init_(self):super(ShowWindow,self)._init_()self.i nitUI()def in itUI(self):self.i nputLabel = QLabel(I nput your text) self.editLi ne = QLi neEdit()self.pri ntButton = QPushButto n(Pri nt) self

6、.clearButton = QPushButt on (Clear) self.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.printBut

7、ton) buttonLayout.addWidget(self.clearButton)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, Emp

8、ty Text,Please enter the letter.)else :QMessageBox.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_()运行的结果: 程序运行后的结果 结合代码和运行

9、的结果,分析代码:Line78 :from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QPushButton, QLineEdit, QVBoxLayout,QMessageBoximport sys导入了程序运行所必须的模块Line10 :class ShowWindow(QWidget):创建一个 ShowWindow 类继承 QWidget ,其中 PyQt 中非常重要的通用窗口类,是所有用户 界面对象的基类,所有和用户界面相关的控件类都是继承自 QWidger 类。Line1214 :def _

10、init_(self):super(ShowWindow,self)._init_() self.initUI()定义了 ShowWindow 类的构造函数 init ,由于 ShowWindow 继承自 QWidgert 类,因此在构 造函数中调用父类 QWidget 的构造函数 super.init() 。同时在构造函数中调用自定义的初始化函数 initUI() ,在该函数中初始化 GUI 中所需各个控 件。Line1720 :self.inputLabel = QLabel(Input your text) self.editLine = QLineEdit() self.printBu

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

12、定义函数 printText() 中的处理逻辑;点击 clearButton 之后调用自定义函数 clearText() 中的处理逻辑。 通过 connect() 方法将点击事件和处理逻辑关联起来这个涉及到 PyQt 信号与槽的概念,信号和槽用于对象之间的通信。当指定事件发生,一个事件信号会被发射。槽可以被任何 Python脚本调用。当和槽连接的信号被发射时,槽会被调用。有关信号与槽的概念,在后续的文章中会专门讲解。Lin e2526: in putLayout = QHBoxLayout()in putLayout.addWidget(self.i nputLabel)in putLayou

13、t.addWidget(self.editL ine)创建一个 inputLayout的水平布局(QHBoxLayout),在inputLayout中添加已定义的控件 inputLabel和editLine。QHBoxLayout让添加的控件水平排布Lin e2931: butt on Layout = QHBoxLayout()butt on Layout.addWidget(self.pri ntButt on)butt on Layout.addWidget(self.clearButt on)同上创建一个 butto nLayout 的水平布局(QHBoxLayout),在 butt

14、on Layout 中添加 prin tButton 和 clearButtonLine3335 :main Layout = QVBoxLayout()main Layout.addLayout(i nputLayout)main Layout.addLayout(butt on Layout)创建一个 mainLayout 的垂直布局(QVBoxLayout),在 mainLayout 中嵌套子布局 inputLayout 和 butt on Layout。通过如下的布局图,进一步了解一下该程序中 layout是如何布局的。t inputLabtl 1 * 邻人杯 ).1电川6术半C: J

15、U CWoi Lnrautd: it有layout的概念,在后续的文章中会专门讲解。Line37 :self.setLayout(main Layout)通过将setLayout()方法,将 main Layout设置为窗口 layout。Line38 :self.setWi ndowTitle(FristWi ndow)通过setWindowTitle()方法设置窗口的标题Line39 :self.show()通过show()方法将窗口显示到屏幕上Line4048 :def prin tText(self):text = self.editL in e.text() if text =:QM

16、essageBox.i nformatio n(self, Empty Text,Please en ter the letter.)else :QMessageBox.i nformati on( self, Pri nt Success,Text: %s % text)定义了处理printButton点击信号的槽函数,当editLine输入框中的文本为空的时候弹出消息 框(QMessageBox),提示Please enter the letter.”;当editLine输入框中的文本不为空时候弹 出消息框,显示 editLine中的文本。触发槽函数printText()之后,当editL

17、ine输入框内容为空时界面显示如下:Lin e4955:def clearText(self):text = self.editL in e.text() if text =:returnelse : self.editLine.clear()定义了处理 clearButton 点击信号的槽函数 ,清空 editLine 输入框中的文本信息。Line 5760 :if _name_ = _main_:app = QApplication(sys.argv)ex = ShowWindow()sys.exit(app.exec_() 程序运行的入口函数类似 C 语言中的 main() 方法。app

18、 = QApplication(sys.argv) ,每一个 pyqt 程序必须创建一个 application 对象, sys.argv 是命令行参数,可以通过命令启动的时候传递参数。ex = ShowWindow() ,创建 ShowWindow 对象。sys.exit(app.exec_(), app.exec_() 事件处理开始,进入程序主循环。主循环等待时间, 并把事件发送给指定处理的任务中。当调用 app.exit() 或者程序因为各种原因被破坏后,使用 sys.exit() 关闭程序,并释放内存资源。到此为止,第一个 PyQt 程序分析结束,可能会存在输入框,布局,等各类控件如何

19、使 用,如何确定对应的控件在那个模块中?在后续的文章中会详细进行介绍。三 基本界面组件之 Button抽象类 QAbstractButton :QAbstractButton作为抽象类,提供 button的通用功能,可按按钮(push button)和可选 择按钮(checkable button )。可选择按钮实现有 QRadioButton和QCheckBox ;可按按钮实现 有 QPushButton 和 QToolButton 。任何一种 button可以显示带文本(.setText()方法设置文本)和图标(.setlcon()设置图标)的 标签。QAbstractButton 提供的

20、状态:1、 isDown() 提示 button 是否按下2、 isChecked()提示button是否已经标记3、 isEnable()提示button是否可以被用户点击4、 isCheckAble() 提示 button 是否为可标记5、 setAutoRepeat()设置button是否在用户长按按钮的时候可以自动重复执行。QAbstractButton 提供的信号:1、 pressed。,当鼠标在butt on上并点击左键的时候 触发信号2、 released(),当鼠标左键被释放的时候触发信号3、 clicked(), 当鼠标首次按下,然后释放,或者快捷键被释放的时候触发信号4、

21、toggled(), 当 button 的标记状态发生改变的时候触发信号接下来会针对每一种 button 进行介绍:QPushButton :class QPushButton(QAbstractButton)| QPushButton(QWidget parent=None)| QPushButton(str, QWidget parent=None)| QPushButton(QIcon, str, QWidget parent=None)由此可见 QPushButton 继承自 QAbstractButton, 是一种 command 按钮。 点击执行一些命 令,或者响应一些问题。常见的

22、诸如“确认” ,“申请”,“取消”,“关闭”,“是”,“否”等按 钮。Command Button 经常通过文本来描述执行的动作。有时候我们也会通过快捷键来执行 对应按钮的命令。通过一个示例对 QPushButton 来进行说明:#-*- coding:utf-8 -*-IIIPushButtonIII_author_ = Tony Zhufrom PyQt5.QtWidgets import QApplication, QWidget, QPushButtonfrom PyQt5.QtGui import QIconfrom PyQt5.QtCore import Qtimport sysc

23、lass PushButton(QWidget):def _init_(self):super(PushButton,self)._init_()self.initUI()def initUI(self):self.setWindowTitle(PushButton) self.setGeometry(400,400,300,260)self.closeButton = QPushButton(self) self.closeButton.setText(Close) #text self.closeButton.setIcon(QIcon(close.png) #icon self.clos

24、eButton.setShortcut(Ctrl+D) #shortcut key self.closeButton.clicked.connect(self.close) self.closeButton.setToolTip(Close the widget) #Tool tip self.closeButton.move(100,100)if _name_ = _main_:app = QApplication(sys.argv)ex = PushButto n() ex.show() sys.exit(app.exec_()运行之后的效果:控件说明:控件类重團标t OILcloEeBu

25、tt onCloseclose, png示例说明:名称为“ Close”的Buttton,点击该Button之后关闭该窗口。 或者通过快捷键 “ Ctrl+C的快捷方式亦可关闭该窗口。代码分析:其他代码部分可以参考上一篇 Pyqt5系列(二 )-第一个PyQt程序中的说明。L2122 :self.closeButto n. setText(Close) #textself.closeButt on. setIc on( QIc on (close.p ng) #ic onsetText()方法,设定button的文本setIcon()方法,设定 button的图标关于butt on文本和图标的

26、显示,也可以通过 QPushButt on的构造函数,在创建对象实 例的时候通过参数直接设定。| QPushButt on (str, QWidget pare nt=No ne)| QPushButton(QIcon, str, QWidget parent=None)L23:self.closeButto n.setShortcut(Ctrl+D) #shortcut key给closeButton设定快捷键方式,即通过 Ctrl+D实现与点击closeButton 一样的功能。L24:self.closeButt on. clicked.c onn ect(self.close)clos

27、eButton点击事件处理的逻辑: 在点击closeButton之后调用 QWidget的close()方法。通过connect。方法将点击事件和处理逻辑关联起来 。L25:self.closeButton.setToolTip(Close the widget)setToolTip()设定提示信息,当鼠标移动到 butt on上时显示” Close the widget ”提示信息。 QToolButton :class QToolButton(QAbstractButton)| QToolButton(QWidget parent=None)同理 QToolButton 继承自 QAbst

28、ractButton 。 QToolButton 就是工具操作相关的按钮 ,通常 和QToolBar搭配使用。QToolButton通常不显示文本, 而显示图标 QIcon。一般QToolButton 会在 QToolBar: addAction 时创建,或者已经存在的 action 添加到 QToolBar 时创建。通过一个示例对 QToolButton 来进行说明:#-*- coding:utf-8 -*-IIIToolButton_author_ = Tony Zhufrom PyQt5.QtWidgets import QApplication, QWidget, QToolButto

29、n, QMainWindow from PyQt5.QtGui import QIconfrom PyQt5.QtCore import Qtimport sysclass ToolButton(QMainWindow):def _init_(self):super(ToolButton,self)._init_() self.initUI()def initUI(self):self.setWindowTitle(ToolButton) self.setGeometry(400,400,300,260)self.toolbar = self.addToolBar(toolBar) self.

30、statusBar()self._detailsbutton = QToolButton() self._detailsbutton.setCheckable(True) self._detailsbutton.setChecked(False)self._detailsbutt on. setArrowType(Qt.RightArrow) self._detailsbutt on. setAutoRaise(True)#self._detailsbutto n.setlc on (QIco n(test.jpg) self._detailsbutto n. setToolButto nSt

31、yle(Qt.ToolButto nIco nOnly) self._detailsbutto n. clicked.c onn ect(self.showDetail) self.toolbar.addWidget(self._detailsbutt on)def showDetail(self):if self._detailsbutt on .isChecked():self.statusBar().showMessage(Show Detail.) else:self.statusBar().showMessage(Close Detail.)if _name_ = _main_: app = QApplicatio n( s

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1