没有合适的资源?快使用搜索试试~ 我知道了~
Pyqt5系列(一).pdf
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 116 浏览量
2022-10-30
12:44:39
上传
评论
收藏 454KB PDF 举报
温馨提示
试读
14页
。。。
资源推荐
资源详情
资源评论
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
win7 32bit
2
、官网下载: 官网上之后对应的
source package
。需要自己编译生成。
3
、
OSDN
下载:
OSDN
上罗列了所有
released
的
PyQt
安装程序,根据开发环境下载了
PyQt5-5.2.1
Note
:
1
、下载
PyQt
时注意选择匹配的
Python
版本和系统的位数;
2
、直接通过
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 = QWidget() w.show()
w.setWindowTitle("Hello 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,QMessageBox
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") 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()
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 == '': return
else : if __name__ ==
'__main__':
app = QApplication(sys.argv) ex = ShowWindow() sys.exit(app.exec_())
运行的结果: 程序运行后的结果 结合代码和运行的结果,分析代码:
Line7~8
:
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QPushButton,
QLineEdit, QVBoxLayout,QMessageBox
import sys
导入了程序运行所必须的模块
Line10
:
class ShowWindow(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("Input your text") self.editLine = QLineEdit()
self.printButton = QPushButton("Print") self.clearButton = QPushButton("Clear")
创建
成员:一个标签(
inputLabel
),输入框(
editLine
),两个按钮(
printButton
,
clearButton
)
Line22~23
:
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
:
def printText(self): 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
输入框中的文本不
为空时候弹 出消息框,显示
editLine
中的文本。
触发槽函数
printText()
之后,当
editLine
输入框内容为空时界面显示如下:
Line49~55:
def clearText(self):
if text == '':
return
else :
定义了处理
clearButton
点击信号的槽函数
,
清空
editLine
输入框中的文本信息。
Line 57~60
:
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ShowWindow()
剩余13页未读,继续阅读
资源评论
- 学习写程序c2023-10-30资源很实用,对我启发很大,有很好的参考价值,内容详细。
G11176593
- 粉丝: 6643
- 资源: 3万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功