Pyqt5系列.docx

上传人:b****6 文档编号:15297698 上传时间:2023-07-03 格式:DOCX 页数:37 大小:535.88KB
下载 相关 举报
Pyqt5系列.docx_第1页
第1页 / 共37页
Pyqt5系列.docx_第2页
第2页 / 共37页
Pyqt5系列.docx_第3页
第3页 / 共37页
Pyqt5系列.docx_第4页
第4页 / 共37页
Pyqt5系列.docx_第5页
第5页 / 共37页
Pyqt5系列.docx_第6页
第6页 / 共37页
Pyqt5系列.docx_第7页
第7页 / 共37页
Pyqt5系列.docx_第8页
第8页 / 共37页
Pyqt5系列.docx_第9页
第9页 / 共37页
Pyqt5系列.docx_第10页
第10页 / 共37页
Pyqt5系列.docx_第11页
第11页 / 共37页
Pyqt5系列.docx_第12页
第12页 / 共37页
Pyqt5系列.docx_第13页
第13页 / 共37页
Pyqt5系列.docx_第14页
第14页 / 共37页
Pyqt5系列.docx_第15页
第15页 / 共37页
Pyqt5系列.docx_第16页
第16页 / 共37页
Pyqt5系列.docx_第17页
第17页 / 共37页
Pyqt5系列.docx_第18页
第18页 / 共37页
Pyqt5系列.docx_第19页
第19页 / 共37页
Pyqt5系列.docx_第20页
第20页 / 共37页
亲,该文档总共37页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Pyqt5系列.docx

《Pyqt5系列.docx》由会员分享,可在线阅读,更多相关《Pyqt5系列.docx(37页珍藏版)》请在冰点文库上搜索。

Pyqt5系列.docx

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的开发,PyQt基于GPL许可开发。

 

PyQt安装:

整体安装的步骤比较简单,首先下载与自己python版本和开发环境一致的PyQt版本。

1、开发环境:

python3.35

win732bit

2、官网下载:

官网上之后对应的sourcepackage。

需要自己编译生成。

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安装的方式比较复杂;

 

功能验证:

安装之后简单写个测试程序验证一下

 

[python]viewplaincopy在CODE上查看代码片派生到我的代码片

#!

/user/bin/python3

#-*-coding:

utf-8-*-

'''''

Creatasimplewindow

'''

__author__='TonyZhu'

importsys

fromPyQt5.QtWidgetsimportQWidget,QApplication

if__name__=='__main__':

app=QApplication(sys.argv)

w=QWidget()

w.show()

w.setWindowTitle("HelloPyQt5")

sys.exit(app.exec_())

运行之后会直接显示一个标题为“HelloPyQt5”的空白窗口。

二第一个PyQt程序

通过下面的一个PyQt5简单例子,来简单了解一下关于如何创建PyQt5的。

具体代码如下:

#-*-coding:

utf-8-*-

'''

FristPyQt5program

'''

__author__='TonyZhu'

fromPyQt5.QtWidgetsimportQApplication,QWidget,QLabel,QHBoxLayout,QPushButton,QLineEdit,QVBoxLayout,QMessageBox

importsys

classShowWindow(QWidget):

def__init__(self):

super(ShowWindow,self).__init__()

self.initUI()

definitUI(self):

self.inputLabel=QLabel("Inputyourtext")

self.editLine=QLineEdit()

self.printButton=QPushButton("Print")

self.clearButton=QPushButton("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.printButton)

buttonLayout.addWidget(self.clearButton)

mainLayout=QVBoxLayout()

mainLayout.addLayout(inputLayout)

mainLayout.addLayout(buttonLayout)

self.setLayout(mainLayout)

self.setWindowTitle('FristWindow')

self.show()

defprintText(self):

text=self.editLine.text()

iftext=='':

QMessageBox.information(self,"EmptyText",

"Pleaseentertheletter.")

else:

QMessageBox.information(self,"PrintSuccess",

"Text:

%s"%text)

defclearText(self):

text=self.editLine.text()

iftext=='':

return

else:

self.editLine.clear()

if__name__=='__main__':

app=QApplication(sys.argv)

ex=ShowWindow()

sys.exit(app.exec_())

运行的结果:

程序运行后的结果

结合代码和运行的结果,分析代码:

Line7~8:

fromPyQt5.QtWidgetsimportQApplication,QWidget,QLabel,QHBoxLayout,QPushButton,QLineEdit,QVBoxLayout,QMessageBox

importsys

导入了程序运行所必须的模块

Line10:

classShowWindow(QWidget):

创建一个ShowWindow类继承QWidget,其中PyQt中非常重要的通用窗口类,是所有用户界面对象的基类,所有和用户界面相关的控件类都是继承自QWidger类。

Line12~14:

def__init__(self):

super(ShowWindow,self).__init__()

self.initUI()

定义了ShowWindow类的构造函数init,由于ShowWindow继承自QWidgert类,因此在构造函数中调用父类QWidget的构造函数super.init()。

同时在构造函数中调用自定义的初始化函数initUI(),在该函数中初始化GUI中所需各个控件。

Line17~20:

self.inputLabel=QLabel("Inputyourtext")

self.editLine=QLineEdit()

self.printButton=QPushButton("Print")

self.clearButton=QPushButton("Clear")

创建成员:

一个标签(inputLabel),输入框(editLine),两个按钮(printButton,clearButton)

Line22~23:

self.printButton.clicked.connect(self.printText)self.clearButton.clicked.connect(self.clearText)

1

printButton和clearButton点击事件处理的逻辑:

点击printButton之后会调用自定义函数printText()中的处理逻辑;点击clearButton之后调用自定义函数clearText()中的处理逻辑。

通过connect()方法将点击事件和处理逻辑关联起来

这个涉及到PyQt信号与槽的概念,信号和槽用于对象之间的通信。

当指定事件发生,一个事件信号会被发射。

槽可以被任何Python脚本调用。

当和槽连接的信号被发射时,槽会被调用。

有关信号与槽的概念,在后续的文章中会专门讲解。

Line25~26:

inputLayout=QHBoxLayout()

inputLayout.addWidget(self.inputLabel)

inputLayout.addWidget(self.editLine)

创建一个inputLayout的水平布局(QHBoxLayout),在inputLayout中添加已定义的控件inputLabel和editLine。

QHBoxLayout让添加的控件水平排布

Line29~31:

buttonLayout=QHBoxLayout()

buttonLayout.addWidget(self.printButton)

buttonLayout.addWidget(self.clearButton)

同上创建一个buttonLayout的水平布局(QHBoxLayout),在buttonLayout中添加printButton和clearButton

Line33~35:

mainLayout=QVBoxLayout()

mainLayout.addLayout(inputLayout)

mainLayout.addLayout(buttonLayout)

创建一个mainLayout的垂直布局(QVBoxLayout),在mainLayout中嵌套子布局inputLayout和buttonLayout。

通过如下的布局图,进一步了解一下该程序中layout是如何布局的。

有layout的概念,在后续的文章中会专门讲解。

Line37:

self.setLayout(mainLayout)

通过将setLayout()方法,将mainLayout设置为窗口layout。

Line38:

self.setWindowTitle('FristWindow')

通过setWindowTitle()方法设置窗口的标题

Line39:

self.show()

通过show()方法将窗口显示到屏幕上

Line40~48:

defprintText(self):

text=self.editLine.text()

iftext=='':

QMessageBox.information(self,"EmptyText",

"Pleaseentertheletter.")

else:

QMessageBox.information(self,"PrintSuccess",

"Text:

%s"%text)

定义了处理printButton点击信号的槽函数,当editLine输入框中的文本为空的时候弹出消息框(QMessageBox),提示“Pleaseentertheletter.”;当editLine输入框中的文本不为空时候弹出消息框,显示editLine中的文本。

触发槽函数printText()之后,当editLine输入框内容为空时界面显示如下:

Line49~55:

defclearText(self):

text=self.editLine.text()

iftext=='':

return

else:

self.editLine.clear()

定义了处理clearButton点击信号的槽函数,清空editLine输入框中的文本信息。

Line57~60:

if__name__=='__main__':

app=QApplication(sys.argv)

ex=ShowWindow()

sys.exit(app.exec_())

程序运行的入口函数类似C语言中的main()方法。

app=QApplication(sys.argv),每一个pyqt程序必须创建一个application对象,sys.argv是命令行参数,可以通过命令启动的时候传递参数。

ex=ShowWindow(),创建ShowWindow对象。

sys.exit(app.exec_()),app.exec_()事件处理开始,进入程序主循环。

主循环等待时间,并把事件发送给指定处理的任务中。

当调用app.exit()或者程序因为各种原因被破坏后,使用sys.exit()关闭程序,并释放内存资源。

到此为止,第一个PyQt程序分析结束,可能会存在输入框,布局,等各类控件如何使用,如何确定对应的控件在那个模块中?

在后续的文章中会详细进行介绍。

三基本界面组件之Button

抽象类QAbstractButton:

QAbstractButton作为抽象类,提供button的通用功能,可按按钮(pushbutton)和可选择按钮(checkablebutton)。

可选择按钮实现有QRadioButton和QCheckBox;可按按钮实现有QPushButton和QToolButton。

任何一种button可以显示带文本(.setText()方法设置文本)和图标(.setIcon()设置图标)的标签。

QAbstractButton提供的状态:

1、isDown()提示button是否按下

2、isChecked()提示button是否已经标记

3、isEnable()提示button是否可以被用户点击

4、isCheckAble()提示button是否为可标记

5、setAutoRepeat()设置button是否在用户长按按钮的时候可以自动重复执行。

QAbstractButton提供的信号:

1、pressed(),当鼠标在button上并点击左键的时候触发信号

2、released(),当鼠标左键被释放的时候触发信号

3、clicked(),当鼠标首次按下,然后释放,或者快捷键被释放的时候触发信号

4、toggled(),当button的标记状态发生改变的时候触发信号

接下来会针对每一种button进行介绍:

QPushButton:

classQPushButton(QAbstractButton)

|QPushButton(QWidgetparent=None)

|QPushButton(str,QWidgetparent=None)

|QPushButton(QIcon,str,QWidgetparent=None)

由此可见QPushButton继承自QAbstractButton,是一种command按钮。

点击执行一些命令,或者响应一些问题。

常见的诸如“确认”,“申请”,“取消”,“关闭”,“是”,“否”等按钮。

CommandButton经常通过文本来描述执行的动作。

有时候我们也会通过快捷键来执行对应按钮的命令。

通过一个示例对QPushButton来进行说明:

#-*-coding:

utf-8-*-

'''

PushButton

'''

__author__='TonyZhu'

fromPyQt5.QtWidgetsimportQApplication,QWidget,QPushButton

fromPyQt5.QtGuiimportQIcon

fromPyQt5.QtCoreimportQt

importsys

classPushButton(QWidget):

def__init__(self):

super(PushButton,self).__init__()

self.initUI()

definitUI(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.closeButton.setShortcut('Ctrl+D')#shortcutkey

self.closeButton.clicked.connect(self.close)

self.closeButton.setToolTip("Closethewidget")#Tooltip

self.closeButton.move(100,100)

if__name__=='__main__':

app=QApplication(sys.argv)

ex=PushButton()

ex.show()

sys.exit(app.exec_())

运行之后的效果:

控件说明:

示例说明:

名称为“Close”的Buttton,点击该Button之后关闭该窗口。

或者通过快捷键“Ctrl+C”的快捷方式亦可关闭该窗口。

代码分析:

其他代码部分可以参考上一篇《Pyqt5系列

(二)-第一个PyQt程序》中的说明。

L21~22:

self.closeButton.setText("Close")#text

self.closeButton.setIcon(QIcon("close.png"))#icon

setText()方法,设定button的文本

setIcon()方法,设定button的图标

关于button文本和图标的显示,也可以通过QPushButton的构造函数,在创建对象实例的时候通过参数直接设定。

|QPushButton(str,QWidgetparent=None)

|QPushButton(QIcon,str,QWidgetparent=None)

L23:

self.closeButton.setShortcut('Ctrl+D')#shortcutkey

给closeButton设定快捷键方式,即通过Ctrl+D实现与点击closeButton一样的功能。

L24:

self.closeButton.clicked.connect(self.close)

closeButton点击事件处理的逻辑:

在点击closeButton之后调用QWidget的close()方法。

通过connect()方法将点击事件和处理逻辑关联起来。

L25:

self.closeButton.setToolTip("Closethewidget")

setToolTip()设定提示信息,当鼠标移动到button上时显示”Closethewidget”提示信息。

QToolButton:

classQToolButton(QAbstractButton)

|QToolButton(QWidgetparent=None)

同理QToolButton继承自QAbstractButton。

QToolButton就是工具操作相关的按钮,通常和QToolBar搭配使用。

QToolButton通常不显示文本,而显示图标QIcon。

一般QToolButton会在QToolBar:

addAction时创建,或者已经存在的action添加到QToolBar时创建。

通过一个示例对QToolButton来进行说明:

#-*-coding:

utf-8-*-

'''

ToolButton

'''

__author__='TonyZhu'

fromPyQt5.QtWidgetsimportQApplication,QWidget,QToolButton,QMainWindow

fromPyQt5.QtGuiimportQIcon

fromPyQt5.QtCoreimportQt

importsys

classToolButton(QMainWindow):

def__init__(self):

super(ToolButton,self).__init__()

self.initUI()

definitUI(self):

self.setWindowTitle("ToolButton")

self.setGeometry(400,400,300,260)

self.toolbar=self.addToolBar("toolBar")

self.statusBar()

self._detailsbutton=QToolButton()

self._detailsbutton.setCheckable(True)

self._detailsbutton.setChecked(False)

self._detailsbutton.setArrowType(Qt.RightArrow)

self._detailsbutton.setAutoRaise(True)

#self._detailsbutton.setIcon(QIcon("test.jpg"))

self._detailsbutton.setToolButtonStyle(Qt.ToolButtonIconOnly)

self._detailsbutton.clicked.connect(self.showDetail)

self.toolbar.addWidget(self._detailsbutton)

defshowDetail(self):

ifself._detailsbutton.isChecked():

self.statusBar().showMessage("ShowDetail....")

else:

self.statusBar().showMessage("CloseDetail....")

if__name__=='__main__':

app=QApplication(sys.argv)

ex=ToolButton()

ex.show()

sys.exit(app.exec_())

运行之后的效果:

控件说明:

示例说明:

图标为“右箭头图标”的Buttton,此按钮有开关之分。

当Button打开之后在消息栏显示“ShowDetail….”,反之显示“CloseDetail”。

代码分析:

其他代码部分可以参考上一篇《Pyqt5系列

(二)-第一个PyQt程序》中的说明。

L24~25:

self._detailsbutto

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2