Pyqt5 基本界面组件之inputDialog的使用


Posted in Python onJune 25, 2019

QInputDialog类提供了一种简单方面的对话框来获得用户的单个输入信息,可以是一个字符串,一个Int类型数据,一个double类型数据或是一个下拉列表框的条目。

对应的Dialog其中包括一个提示标签,一个输入控件(若是调用字符串输入框,则为一个QLineEdit,若是调用Int类型或double类型,则为一个QSpinBox,若是调用列表条目输入框,则为一个QComboBox),还包括一个确定输入(Ok)按钮和一个取消输入(Cancel)按钮。

QInputDialog:

class QInputDialog(QDialog)
 | QInputDialog(QWidget parent=None, Qt.WindowFlags flags=0)

QInputDialog同样继承自QDialog,提供简单输入的对话框,

代码示例 :

示例代码如下:

#-*- coding:utf-8 -*-
'''
inputDialog
'''
__author__ = 'Tony Zhu'

from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QInputDialog, QGridLayout, QLabel, QPushButton, QFrame

class InputDialog(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")
    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(QFrame.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("...")
    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.nameLable,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)
    mainLayout.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,"项目名称","输入项目名称:",
                    QLineEdit.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,"项目成员","请输入项目成员人数:",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 = QInputDialog.getMultiLineText(self,"项目介绍","介绍:","服务外包第三方公司 \nPython project")
    if ok :
      self.introductionLable.setText(introduction)



if __name__=="__main__":
  import sys
  app=QApplication(sys.argv)
  myshow=InputDialog()
  myshow.show()
  sys.exit(app.exec_())

运行之后的效果:

Pyqt5 基本界面组件之inputDialog的使用

示例说明:

通过点击不同的按钮,来选择不同类型的输入对话框,从而选择所需的数据。

代码分析:

L18~22:

label1=QLabel("项目名称:")
    label2=QLabel("项目类型:")
    label3=QLabel("项目人员:")
    label4=QLabel("项目成本:")
    label5=QLabel("项目介绍:")

定义了数据项名称的标签。

L24~33:

self.nameLable = QLabel("PyQt5")
    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(QFrame.Panel|QFrame.Sunken)
    self.introductionLable = QLabel("服务外包第三方公司")
    self.introductionLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)

定义了项目数据项中的数据内容,数据内容显示在对应的标签中。

setFrameStyle()设定标签的样式,有如下的样式:

QFrame.Box

QFrame.Panel

QFrame.WinPanel

QFrame.HLine

QFrame.VLine

QFrame.StyledPanel

QFrame.Sunken

QFrame.Raised

L35~L44:

nameButton=QPushButton("...")
    nameButton.clicked.connect(self.selectName)
    styleButton=QPushButton("...")
    styleButton.clicked.connect(self.selectStyle)
    numberButton=QPushButton("...")
    numberButton.clicked.connect(self.selectNumber)
    costButton=QPushButton("...")
    costButton.clicked.connect(self.selectCost)
    introductionButton=QPushButton("...")
    introductionButton.clicked.connect(self.selectIntroduction)

实例化QPushButton对象,并将对应的clicked信号和自定义的槽函数绑定起来。

L46~61:

实例化网格布局,并将对应的控件添加到网格布局中。

功能分析:

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()函数原型如下:

| getText(...)
 |   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指定标准输入对话框中QLineEdit控件的输入模式;

第5个参数str,标准输入对话框中QLineEdit控件的默认值;

第6个参数flags,指明标准输入对话框的窗体标识;

第7个参数inputMethodHints,通过选择不同的inputMethodHints值来实现不同的键盘布局;

单击nameButton之后的效果:

Pyqt5 基本界面组件之inputDialog的使用

若用户单击了“OK”按钮,则把新输入的名称更新至显示标签。

2:获取项目属性:

def selectStyle(self):
    list = ["外包","自研"]
    style,ok = QInputDialog.getItem(self,"项目性质","请选择项目性质:",list)
    if ok :
      self.styleLable.setText(style)

调用QInputDialog的getItem()函数弹出标准条目选择对话框,getItem()函数原型如下:

| getItem(...)
 |   QInputDialog.getItem(QWidget, str, str, list-of-str, int current=0, bool editable=True, Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) -> (str, bool)

第1个参数parent,用于指定父组件;

第2个参数str,是标准条目选择对话框的标题名;

第3个参数str,标准条目选择对话框的标签提示;

第4个参数list-of-str,标准条目选择对话框中对应条目的list;

第5个参数editable,标准条目选择对话框条目是否可编辑标识,默认为不可编辑;

第6个参数flags,指明标准输入对话框的窗体标识;

第7个参数inputMethodHints,通过选择不同的inputMethodHints值来实现不同的键盘布局.;

单击styleButton之后的效果:

Pyqt5 基本界面组件之inputDialog的使用

若用户单击了“OK”按钮,则把新选择的类型更新至显示标签。

3:获取项目成员:

def selectNumber(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, 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中上下选择是数据变化的步长;

第8个参数inputMethodHints,通过选择不同的inputMethodHints值来实现不同的键盘布局;

单击numberButton之后的效果:

Pyqt5 基本界面组件之inputDialog的使用

若用户单击了“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))

调用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,输入对话框的标签提示;

第4个参数value,标准float类型输入对话框中的默认值;

第5个参数min,标准float类型输入对话框中的最小值;

第6个参数max,标准float类型输入对话框中的最大值;

第7个参数decimals,小数点后面保留的位数;

第8个参数inputMethodHints,通过选择不同的inputMethodHints值来实现不同的键盘布局;

单击costButton之后的效果:

Pyqt5 基本界面组件之inputDialog的使用

若用户单击了“OK”按钮,则把新选择的成本数据更新至显示标签

5:获取项目介绍:

def selectIntroduction(self):
    introduction,ok = QInputDialog.getMultiLineText(self,"项目介绍","介绍:","服务外包第三方公司 \nPython project")
    if ok :
      self.introductionLable.setText(introduction)

调用QInputDialog的getMultiLineText()函数弹出标准多行文本类型输入对话框,getMultiLineText()函数原型如下:

| getMultiLineText(...)
 |   QInputDialog.getMultiLineText(QWidget, str, str, str text='', 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之后的效果:

Pyqt5 基本界面组件之inputDialog的使用

若用户单击了“OK”按钮,则把新修改的项目介绍信息更新至显示标签

以上这篇Pyqt5 基本界面组件之inputDialog的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python获得图片base64编码示例
Jan 16 Python
python中字符串类型json操作的注意事项
May 02 Python
Numpy掩码式数组详解
Apr 17 Python
Python使用Matplotlib模块时坐标轴标题中文及各种特殊符号显示方法
May 04 Python
Python中的Numpy矩阵操作
Aug 12 Python
用Python将结果保存为xlsx的方法
Jan 28 Python
用Python逐行分析文件方法
Jan 28 Python
Python中Numpy ndarray的使用详解
May 24 Python
django在保存图像的同时压缩图像示例代码详解
Feb 11 Python
python能自学吗
Jun 18 Python
Sublime Text3最新激活注册码分享适用2020最新版 亲测可用
Nov 12 Python
python pyhs2 的安装操作
Apr 07 Python
对PyQt5的输入对话框使用(QInputDialog)详解
Jun 25 #Python
如何使用Python标准库进行性能测试
Jun 25 #Python
python绘制评估优化算法性能的测试函数
Jun 25 #Python
Python基于机器学习方法实现的电影推荐系统实例详解
Jun 25 #Python
Python 中的参数传递、返回值、浅拷贝、深拷贝
Jun 25 #Python
pyqt5 删除layout中的所有widget方法
Jun 25 #Python
在Python中表示一个对象的方法
Jun 25 #Python
You might like
用Php实现链结人气统计
2006/10/09 PHP
php输出xml必须header的解决方法
2014/10/17 PHP
php使用GD2绘制几何图形示例
2017/02/15 PHP
通过jQuery源码学习javascript(二)
2012/12/27 Javascript
js实现倒计时(距离结束还有)示例代码
2013/07/24 Javascript
javascript 获取图片尺寸及放大图片
2013/09/04 Javascript
js中的hasOwnProperty和isPrototypeOf方法使用实例
2014/06/06 Javascript
一个jquery实现的不错的多行文字图片滚动效果
2014/09/28 Javascript
排序算法的javascript实现与讲解(99js手记)
2014/09/28 Javascript
js对字符的验证方法汇总
2015/02/04 Javascript
jQuery获取table行数并输出单元格内容的实现方法
2016/06/30 Javascript
js显示动态时间的方法详解
2016/08/20 Javascript
如何利用模板将HTML从JavaScript中抽离
2016/10/08 Javascript
js实现图片加载淡入淡出效果
2017/04/07 Javascript
vue 实现滚动到底部翻页效果(pc端)
2019/07/31 Javascript
layui导出所有数据的例子
2019/09/10 Javascript
layui(1.0.9)文件上传upload,前后端的实例代码
2019/09/26 Javascript
JS实现按比例缩小图片宽高
2020/08/24 Javascript
[01:46]辉夜杯—打造中国DOTA新格局
2015/12/25 DOTA
[50:17]Newbee vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
[53:52]OG vs EG 2018国际邀请赛淘汰赛BO3 第二场 8.23
2018/08/24 DOTA
从零学Python之入门(五)缩进和选择
2014/05/27 Python
python比较两个列表大小的方法
2015/07/11 Python
Django ORM框架的定时任务如何使用详解
2017/10/19 Python
Python通过Django实现用户注册和邮箱验证功能代码
2017/12/11 Python
python中copy()与deepcopy()的区别小结
2018/08/03 Python
python简单鼠标自动点击某区域的实例
2019/06/25 Python
PyCharm下载和安装详细步骤
2019/12/17 Python
python爬虫开发之selenium模块详细使用方法与实例全解
2020/03/09 Python
CSS3制作ajax loader icon实现思路及代码
2013/08/25 HTML / CSS
加拿大最大的相机店:Henry’s
2017/05/17 全球购物
FORZIERI福喜利中国官网:奢侈品购物梦工厂
2019/05/03 全球购物
入党自我评价优缺点
2014/01/25 职场文书
党校培训自我鉴定范文
2014/03/20 职场文书
元宵节晚会主持人串词
2014/03/25 职场文书
高二学生评语大全
2014/04/25 职场文书