收藏 分享(赏)

Pyqt5系列(二).doc

上传人:weiwoduzun 文档编号:2611820 上传时间:2018-09-23 格式:DOC 页数:50 大小:1,016.50KB
下载 相关 举报
Pyqt5系列(二).doc_第1页
第1页 / 共50页
Pyqt5系列(二).doc_第2页
第2页 / 共50页
Pyqt5系列(二).doc_第3页
第3页 / 共50页
Pyqt5系列(二).doc_第4页
第4页 / 共50页
Pyqt5系列(二).doc_第5页
第5页 / 共50页
点击查看更多>>
资源描述

1、Pyqt5 系列(二)五 基本界面组件之 inputDialogQInputDialog 类提供了一种简单方面的对话框来获得用户的单个输入信息,可以是一个字符串,一个 Int 类型数据,一个 double 类型数据或是一个下拉列表框的条目。 对应的 Dialog 其中包括一个提示标签,一个输入控件(若是调用字符串输入框,则为一个QLineEdit,若是调用 Int 类型或 double 类型,则为一个 QSpinBox,若是调用列表条目输入框,则为一个 QComboBox) ,还包括一个确定输入(Ok) 按钮和一个取消输入(Cancel)按钮。QInputDialog:class QInput

2、Dialog(QDialog)| QInputDialog(QWidget parent=None, Qt.WindowFlags flags=0)QInputDialog 同样继承自 QDialog,提供简单输入的对话框,代码示例 :示例代码如下:#-*- coding:utf-8 -*-inputDialog_author_ = Tony Zhufrom PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QInputDialog, QGridLayout, QLabel, QPushButton, QFrameclass In

3、putDialog(QWidget):def _init_(self): super(InputDialog,self)._init_()self.initUi()def initUi(self):self.setWindowTitle(“项目信息“)self.setGeometry(400,400,300,260)label1=QLabel(“项目名称:“)label2=QLabel(“项目类型:“)label3=QLabel(“项目人员:“)label4=QLabel(“项目成本:“)label5=QLabel(“项目介绍:“)self.nameLable = QLabel(“PyQt5“

4、)self.nameLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.styleLable = QLabel(“外包“)self.styleLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.numberLable = QLabel(“40“)self.numberLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.costLable = QLabel(“400.98“)self.costLable.setFrameStyle(QFram

5、e.Panel|QFrame.Sunken)self.introductionLable = QLabel(“服务外包第三方公司“)self.introductionLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)nameButton=QPushButton(“.“)nameButton.clicked.connect(self.selectName)styleButton=QPushButton(“.“)styleButton.clicked.connect(self.selectStyle)numberButton=QPushButton(“.

6、“)numberButton.clicked.connect(self.selectNumber)costButton=QPushButton(“.“)costButton.clicked.connect(self.selectCost)introductionButton=QPushButton(“.“)introductionButton.clicked.connect(self.selectIntroduction)mainLayout=QGridLayout()mainLayout.addWidget(label1,0,0)mainLayout.addWidget(self.nameL

7、able,0,1)mainLayout.addWidget(nameButton,0,2)mainLayout.addWidget(label2,1,0)mainLayout.addWidget(self.styleLable,1,1)mainLayout.addWidget(styleButton,1,2)mainLayout.addWidget(label3,2,0)mainLayout.addWidget(self.numberLable,2,1)mainLayout.addWidget(numberButton,2,2)mainLayout.addWidget(label4,3,0)m

8、ainLayout.addWidget(self.costLable,3,1)mainLayout.addWidget(costButton,3,2)mainLayout.addWidget(label5,4,0)mainLayout.addWidget(self.introductionLable,4,1)mainLayout.addWidget(introductionButton,4,2)self.setLayout(mainLayout)def selectName(self):name,ok = QInputDialog.getText(self,“项目名称“,“输入项目名称:“,Q

9、LineEdit.Normal,self.nameLable.text()if ok and (len(name)!=0):self.nameLable.setText(name)def selectStyle(self):list = “外包“,“自研“style,ok = QInputDialog.getItem(self,“项目性质“,“请选择项目性质:“,list)if ok :self.styleLable.setText(style)def selectNumber(self):number,ok = QInputDialog.getInt(self,“项目成员“,“ 请输入项目成

10、员人数:“,int(self.numberLable.text(),20,100,2)if ok :self.numberLable.setText(str(number)def selectCost(self):cost,ok = QInputDialog.getDouble(self,“项目成本“,“ 请输入项目成员人数:“,float(self.costLable.text(),100.00,500.00,2)if ok :self.costLable.setText(str(cost)def selectIntroduction(self):introduction,ok = QInp

11、utDialog.getMultiLineText(self,“项目介绍“,“介绍:“,“ 服务外包第三方公司 nPython project“)if ok :self.introductionLable.setText(introduction)if _name_=“_main_“:import sysapp=QApplication(sys.argv)myshow=InputDialog()myshow.show()sys.exit(app.exec_()示例说明: 通过点击不同的按钮,来选择不同类型的输入对话框,从而选择所需的数据。 代码分析: L1822:label1=QLabel(“

12、项目名称:“)label2=QLabel(“项目类型:“)label3=QLabel(“项目人员:“)label4=QLabel(“项目成本:“)label5=QLabel(“项目介绍:“)定义了数据项名称的标签。L2433:self.nameLable = QLabel(“PyQt5“)self.nameLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.styleLable = QLabel(“外包“)self.styleLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.numberL

13、able = QLabel(“40“)self.numberLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.costLable = QLabel(“400.98“)self.costLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.introductionLable = QLabel(“服务外包第三方公司“)self.introductionLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)定义了项目数据项中的数据内容,数据内容显示在对应的标

14、签中。 setFrameStyle()设定标签的样式,有如下的样式: QFrame.Box QFrame.Panel QFrame.WinPanel QFrame.HLine QFrame.VLine QFrame.StyledPanel QFrame.Sunken QFrame.RaisedL35L44:nameButton=QPushButton(“.“)nameButton.clicked.connect(self.selectName)styleButton=QPushButton(“.“)styleButton.clicked.connect(self.selectStyle)num

15、berButton=QPushButton(“.“)numberButton.clicked.connect(self.selectNumber)costButton=QPushButton(“.“)costButton.clicked.connect(self.selectCost)introductionButton=QPushButton(“.“)introductionButton.clicked.connect(self.selectIntroduction)实例化 QPushButton 对象,并将对应的 clicked 信号和自定义的槽函数绑定起来。L4661: 实例化网格布局,

16、并将对应的控件添加到网格布局中。功能分析:1:获取项目名称:def selectName(self):name,ok = QInputDialog.getText(self,“项目名称“,“输入项目名称:“, QLineEdit.Normal,self.nameLable.text()if ok and (len(name)!=0):self.nameLable.setText(name)QInputDialog 中很多方法均为静态方法,因此不需要实例化直接可以调用。调用QInputDialog 的 getText()函数弹出标准字符串输入对话框, getText()函数原型如下:| getT

17、ext(.)| QInputDialog.getText(QWidget, str, str, QLineEdit.EchoMode echo=QLineEdit.Normal, str text=QString(), Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) - (str, bool)第 1 个参数 parent,用于指定父组件;第 2 个参数 str,是标准输入对话框的标题名;第 3 个参数 str,标准输入对话框的标签提示;第 4 个参数 echo,mode 指定标准输入对话框中 QL

18、ineEdit 控件的输入模式;第 5 个参数 str,标准输入对话框中 QLineEdit 控件的默认值;第 6 个参数 flags,指明标准输入对话框的窗体标识;第 7 个参数 inputMethodHints,通过选择不同的 inputMethodHints 值来实现不同的键盘布局;单击 nameButton 之后的效果:若用户单击了“OK”按钮,则把新输入的名称更新至显示标签。2:获取项目属性:def selectStyle(self):list = “外包“,“自研“style,ok = QInputDialog.getItem(self,“项目性质“,“请选择项目性质:“,list

19、)if ok :self.styleLable.setText(style)调用 QInputDialog 的 getItem()函数弹出标准条目选择对话框,getItem()函数原型如下:| getItem(.)| QInputDialog.getItem(QWidget, str, str, list-of-str, int current=0, booleditable=True, Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) - (str, bool)第 1 个参数 parent,用于指

20、定父组件;第 2 个参数 str,是标准条目选择对话框的标题名;第 3 个参数 str,标准条目选择对话框的标签提示;第 4 个参数 list-of-str,标准条目选择对话框中对应条目的 list;第 5 个参数 editable,标准条目选择对话框条目是否可编辑标识,默认为不可编辑;第 6 个参数 flags,指明标准输入对话框的窗体标识;第 7 个参数 inputMethodHints,通过选择不同的 inputMethodHints 值来实现不同的键盘布局 ;单击 styleButton 之后的效果:若用户单击了“OK”按钮,则把新选择的类型更新至显示标签。3:获取项目成员:def s

21、electNumber(self):number,ok = QInputDialog.getInt(self,“项目成员“,“ 请输入项目成员人数:“,int(self.numberLable.text(),20,100,2)if ok :self.numberLable.setText(str(number)调用 QInputDialog 的 getInt()函数弹出标准 int 类型输入对话框,getInt()函数原型如下:| getInt(.)| QInputDialog.getInt(QWidget, str, str, int value=0, int min=-2147483647

22、, int max=2147483647, int step=1, Qt.WindowFlags flags=0) - (int, bool)第 1 个参数 parent,用于指定父组件;第 2 个参数 str,是标准 int 类型输入对话框的标题名;第 3 个参数 str,标准 int 类型输入对话框的标签提示;第 4 个参数 value,标准 int 类型输入对话框中的默认值;第 5 个参数 min,标准 int 类型输入对话框中的最小值;第 6 个参数 max,标准 int 类型输入对话框中的最大值;第 7 个参数 step,标准 int 类型输入对话框中的步长,即 QSpinBox 中

23、上下选择是数据变化的步长;第 8 个参数 inputMethodHints,通过选择不同的 inputMethodHints 值来实现不同的键盘布局;单击 numberButton 之后的效果:若用户单击了“OK”按钮,则把新选择的成员数据更新至显示标签。4:获取项目成本:def selectCost(self):cost,ok = QInputDialog.getDouble(self,“项目成本“,“ 请输入项目成员人数:“,float(self.costLable.text(),100.00,500.00,2)if ok :self.costLable.setText(str(cost)

24、调用 QInputDialog 的 getDouble()函数弹出标准 float 类型输入对话框, getDouble()函数原型如下:| getDouble(.)| QInputDialog.getDouble(QWidget, str, str, float value=0, float min=-2147483647, float max=2147483647, int decimals=1, Qt.WindowFlags flags=0) - (float, bool)第 1 个参数 parent,用于指定父组件;第 2 个参数 str,输入对话框的标题名;第 3 个参数 str,输

25、入对话框的标签提示;第 4 个参数 value,标准 float 类型输入对话框中的默认值;第 5 个参数 min,标准 float 类型输入对话框中的最小值;第 6 个参数 max,标准 float 类型输入对话框中的最大值;第 7 个参数 decimals,小数点后面保留的位数;第 8 个参数 inputMethodHints,通过选择不同的 inputMethodHints 值来实现不同的键盘布局;单击 costButton 之后的效果:若用户单击了“OK”按钮,则把新选择的成本数据更新至显示标签5:获取项目介绍:def selectIntroduction(self):introduc

26、tion,ok = QInputDialog.getMultiLineText(self,“项目介绍“,“介绍:“,“ 服务外包第三方公司 nPython project“)if ok :self.introductionLable.setText(introduction)调用 QInputDialog 的 getMultiLineText()函数弹出标准多行文本类型输入对话框,getMultiLineText()函数原型如下:| getMultiLineText(.)| QInputDialog.getMultiLineText(QWidget, str, str, str text=,

27、Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) - (str, bool)第 1 个参数 parent,用于指定父组件;第 2 个参数 str,输入对话框的标题名;第 3 个参数 str,输入对话框的标签提示;第 4 个参数 text,输入对话框中 LineEdit 的默认值;第 5 个参数 flags,指明标准输入对话框的窗体标识;第 6 个参数 inputMethodHints,通过选择不同的 inputMethodHints 值来实现不同的键盘布局;单击 introductionButton

28、 之后的效果:若用户单击了“OK”按钮,则把新修改的项目介绍信息更新至显示标签六 基本界面组件之 MessageBox消息框针对某个场景以文本的形式向用户进行提示,为了获取用户的响应消息框可以显示图标和标准按钮。在实际的界面交互中,经常会看到各种类型的消息框,显示关于消息框,显示严重错误消息框,显示警告消息框等等。由于这些对话框在各个程序中都是一样的,所以 QT 中就统一提供了一个 QMessageBox 的类,这样在所有程序中都可以直接使用。QMessageBox 提供两套接口来实现,一种是 static functions(静态方法调用) ,另外一种 the property-base A

29、PI(基于属性的 API) 。直接调用静态方法是一种比较简单的途径,但是没有基于属性 API 的方式灵活。在 QT 的官网上推荐使用 the property-base API。Static functions 方式:QMessageBox 用于显示消息提示。一般会使用到其提供的几个 static 函数(C+层的函数原型,其参数类型和 python 中的一样):通过一个示例来进行说明各个方法的使用:#-*- coding:utf-8 -*-MessageBox_author_ = Tony Zhufrom PyQt5.QtWidgets import QApplication, QWidget

30、, QLineEdit, QMessageBox, QGridLayout, QLabel, QPushButton, QFrameclass MessageBox(QWidget):def _init_(self): super(MessageBox,self)._init_()self.initUi()def initUi(self):self.setWindowTitle(“MessageBox“)self.setGeometry(400,400,300,290)self.questionLabel = QLabel(“Question:“)self.questionLabel.setF

31、rameStyle(QFrame.Panel|QFrame.Sunken)self.infoLabel = QLabel(“Information:“)self.infoLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.warningLabel = QLabel(“Warning:“)self.warningLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.criticalLabel = QLabel(“Critical:“)self.criticalLabel.setFrameStyl

32、e(QFrame.Panel|QFrame.Sunken)self.aboutLabel = QLabel(“About:“)self.aboutLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.aboutQtLabel = QLabel(“About QT:“)self.aboutQtLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)self.resultLabel = QLabel(“Result:“)self.resultLabel.setFrameStyle(QFrame.Panel|QF

33、rame.Sunken)questButton=QPushButton(“.“)questButton.clicked.connect(self.selectQuestion)infoButton=QPushButton(“.“)infoButton.clicked.connect(self.selectInfo)warningButton=QPushButton(“.“)warningButton.clicked.connect(self.selectWarning)criticalButton=QPushButton(“.“)criticalButton.clicked.connect(s

34、elf.selectCritical)aboutButton=QPushButton(“.“)aboutButton.clicked.connect(self.selectAbout)aboutQtButton=QPushButton(“.“)aboutQtButton.clicked.connect(self.selectAboutQt)mainLayout=QGridLayout()mainLayout.addWidget(self.questionLabel,0,0)mainLayout.addWidget(questButton,0,1) mainLayout.addWidget(se

35、lf.infoLabel,1,0)mainLayout.addWidget(infoButton,1,1)mainLayout.addWidget(self.warningLabel,2,0)mainLayout.addWidget(warningButton,2,1)mainLayout.addWidget(self.criticalLabel,3,0)mainLayout.addWidget(criticalButton,3,1)mainLayout.addWidget(self.aboutLabel,4,0)mainLayout.addWidget(aboutButton,4,1)mai

36、nLayout.addWidget(self.aboutQtLabel,5,0)mainLayout.addWidget(aboutQtButton,5,1)mainLayout.addWidget(self.resultLabel,6,1)self.setLayout(mainLayout)def selectQuestion(self):button = QMessageBox.question(self,“Question“,“检测到程序有更新,是否安装最新版本?“,QMessageBox.Ok|QMessageBox.Cancel,QMessageBox.Ok)if button =

37、QMessageBox.Ok:self.resultLabel.setText(“Question: OK“)elif button = QMessageBox.Cancel:self.resultLabel.setText(“Question: Cancel“)else:returndef selectInfo(self):QMessageBox.information(self,“Information“,“程序当前版本为 V3.11“)self.resultLabel.setText(“Information“)4def selectWarning(self):button = QMes

38、sageBox.warning(self,“Warning“,“恢复出厂设置将导致用户数据丢失,是否继续操作?“,QMessageBox.Reset|QMessageBox.Help|QMessageBox.Cancel,QMessageBox.Reset)if button = QMessageBox.Reset:self.resultLabel.setText(“Warning: Reset“)elif button = QMessageBox.Help:self.resultLabel.setText(“Warning: Help“)elif button = QMessageBox.C

39、ancel:self.resultLabel.setText(“Warning: Cancel“)else:returndef selectCritical(self):QMessageBox.critical(self,“Critical“,“服务器宕机!“)self.resultLabel.setText(“Critical“)def selectAbout(self):QMessageBox.about(self,“About“,“Copyright 2015 Tony zhu.n All Right reserved.“)self.resultLabel.setText(“About“

40、)def selectAboutQt(self):QMessageBox.aboutQt(self,“About Qt“)self.resultLabel.setText(“About Qt“)if _name_=“_main_“:import sysapp=QApplication(sys.argv)myshow=MessageBox()myshow.show()sys.exit(app.exec_()示例说明: 通过点击不同的按钮,来选择不同类型的消息框,并将处理的结果显示在 resultLabel中。 代码分析: L1932:self.questionLabel = QLabel(“Qu

41、estion:“) self.questionLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken).self.resultLabel=QLabel(“Result:“) self.resultLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)定义不同消息类型的标签,其中 resultLabel 显示不同 message box 执行的结果。L3445:questButton=QPushButton(“.“)questButton.clicked.connect(self.selectQuestion).abo

42、utQtButton=QPushButton(“.“)aboutQtButton.clicked.connect(self.selectAboutQt)定义针对不同类型操作的按钮,并且将对应按钮的 clicked 信号和自定义的槽函数绑定在一起。L4760:mainLayout=QGridLayout()mainLayout.addWidget(self.questionLabel,0,0)mainLayout.addWidget(questButton,0,1) .mainLayout.addWidget(self.aboutQtLabel,5,0)mainLayout.addWidget(

43、aboutQtButton,5,1)mainLayout.addWidget(self.resultLabel,6,1)实例化网格布局,并将定义的标签和按钮添加到布局的对应位置。1、question 类型 question 类型的执行代码段如下:def selectQuestion(self):button = QMessageBox.question(self,“Question“,“检测到程序有更新,是否安装最新版本?“,QMessageBox.Ok|QMessageBox.Cancel,QMessageBox.Ok)if button = QMessageBox.Ok:self.resu

44、ltLabel.setText(“Question: OK“)elif button = QMessageBox.Cancel:self.resultLabel.setText(“Question: Cancel“)else:return调用 static 方法 question()生产 question 类型的消息框(Ok 和 cancel 按钮,默认选择Ok 按钮) ,该执行之后返回用户选择的按钮。通过判断返回的按钮类型,在 resultLabel 中显示对应的内容。question(QWidget *parent, const QString msgBox.setInformativeT

45、ext(“Do you want to save your changes?“);msgBox.setDetailedText(“Python is powerful. and fast; nplays well with others;n runs everywhere; n is friendly nis Open.“)msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel);msgBox.setDefaultButton(QMessageBox.Save);ret = ms

46、gBox.exec()if ret = QMessageBox.Save:self.displayLabel.setText(“Save“)elif ret = QMessageBox.Discard:self.displayLabel.setText(“Discard“)elif ret = QMessageBox.Cancel:self.displayLabel.setText(“Cancel“)else:passif _name_=“_main_“:import sysapp=QApplication(sys.argv)myshow=MessageBox()myshow.show()sy

47、s.exit(app.exec_()执行的结果:示例说明: 程序启动的时候直接显示自定义的 messagebox,并且在 resultLabel 中显示选择的结果。代码分析: L26: msgBox.setWindowTitle(“The property-base API”) 通过 setWindowTitle()设定消息框的标题。L27: msgBox.setText(“The Python file has been modified.”); 通过 setText()设定消息框的提示信息。L28: msgBox.setInformativeText(“Do you want to sav

48、e your changes?”) setInformativeText(),在对话框中显示的简单说明文字L29: msgBox.setDetailedText(“Python is powerful and fast; nplays well with others;n runs everywhere; n is friendly nis Open.”)setDetailedText(),设定消息框中的详细文本信息。当设定详细文本的时候,对话框自动增加“Show Details”按钮。通过上述的例子,可以发现通过 the property-base API 的方式,可以更灵活的定义messa

49、gebox。QMessageBox 常用属性:七 信号与槽机制信号和槽机制是 QT 的核心机制,要精通 QT 编程就必须对信号和槽有所了解。信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重要地方。 在 linux、windows 等 GUI 工具包中,GUI 组件都会注册回调函数用于处理组件所触发的动作,通常是注册对应的函数的函数指针。在之前关于 Button 的文章中提到了信号与槽的机制的使用,通过该机制可以很好的将组件的信号(如 button 的clocked、toggled、pressed 等)和处理该信号的槽关联起来。通过 信号与槽机制,能够让我们很简洁和快速的来完成相关的功能。信号和槽是用来在对象间传递数据的方法:当一个特定事件发生的时候,signal 会被emit 出来, slot 调用是用来响应相应的 signal 的。Qt 中对象已经包含了许多预定义的 signal(基本组件都有各自特有的预定义的信号) ,根据使用的场景我们可以添加新的signal

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

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报