对PyQt5的输入对话框使用(QInputDialog)详解


Posted in Python onJune 25, 2019

PyQt5中QInputDialog的使用,Qt的QInputDialog类提供了一种简单方面的对话框来获得用户的单个输入信息,它提供了4种数据类型的输入:

1)字符串型(方法=QInputDialog.getText);

2)Int类型数据(方法=QInputDialog.getInt);

3)double类型数据(方法=QInputDialog.getDouble);

4)下拉列表框的条目(方法=QInputDialog.getItem)。

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

class QInputDialog(QDialog)

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

QInputDialog简介:

Qt提供了一个QInputDialog类,QInputDialogDialog类提供了一种简单方便的对话框来获得用户的单个输入信息,目前提供了4种数据类型的输入,可以使一个字符串、一个Int类型数据、一个double类型数据或者是一个下拉列表框的条目。一个标准输入对话框的基本结构如下图所示:

其中包含一个提示标签,一个输入控件。如实调用字符串输入框,则为一个QLineEdit;若是调用Int类型或都报了类型输入框,则为一个QSpinBox;若是调用列表条目输入框,则为一个QComboBox;还包括一个确定输入(OK)按钮和一个取消输入(Cancel)按钮。

QInputDialog的静态函数

1、getText()

QInputDialog的getText()函数弹出标准字符串输入对话框,getText()函数原型如下:

QString getText( QWidget * parent, #标准输入对话框的父窗口

const QString & title, #输入对话框的标题名

const QString & label,#标准输入对话框的标签提示

const QString & text = QString(), #标准字符串输入对话框弹出时QLineEdit控件中默认出现的文字

bool * ok = 0, #用于指示标准输入对话框的哪个按钮被触发,若ok为true,则表示用户单击了OK(确定)按钮,若ok为false,则表示用户单击了Cancel(取消)按钮

Qt::WindowFlags flags = 0, #知名标准输入对话框的窗体标识

Qt::InputMethodHints inputMethodHints = Qt::ImhNone ); [static]

2、getItem()

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

QString getItem( QWidget * parent, 标准输入对话框的父窗口

const QString & title, 标准输入对话框的标题名

const QString & label, 标准输入对话框的标签提示

const QStringList & list, 指定标准输入对话框中QComboBox控件显示的可选条目,为一个QStringList对象

int current = 0, 指定标准输入对话框中QComboBox控件显示的可选条目,为一个QStringList对象

bool editable = true, 指定QComboBox控件中显示的文字是否可编辑;

bool * ok = 0, 用于指定标准输入对话框的哪个那妞被触发,若ok为false,则表示用户单击了Cancel(取消)按钮;

Qt::WindowFlags f = 0 ) ; [static]用于指定标准输入对话框的哪个那妞被触发,若ok为false,则表示用户单击了Cancel(取消)按钮;

3、getInteger()

QInputDialog的getInteger()函数弹出标准int类型输入对话框,getInteger()函数原型如下:

int getInteger( QWidget * parent, 父窗口

const QString & title,标题名

const QString & label, 标签提示

int value = 0, 指定标准输入对话框中QSpinBox控件默认显示值

int minValue = -2147483647,

int maxValue = 2147483647, 指定QSpinBoxBox控件的数值范围,最小和最大值

int step = 1, step指定QSpinBox控件的步进值(即步长)

bool * ok = 0,

Qt::WindowFlags f = 0 ) ;

4、getDouble()

QInputDialog的getDouble()函数弹出标准double类型输入对话框,getDouble()函数原型如下:

double getDouble( QWidget * parent,

const QString & title,

const QString & label,标签提示

double value = 0, 指定标准输入对话框中QSpinBox控件默认显示值

double minValue = -2147483647,

double maxValue 2147483647,

int decimals = 1, 指定QSpinBox控件的浮动数的小数点位数

bool * ok = 0,

Qt::WindowFlags f = 0 ) ;

例子

1)字符串

def getText(self):
 text, okPressed = QInputDialog.getText(self, "Get text","Your name:", QLineEdit.Normal, "")
 if okPressed and text != '':
  print(text)

2)int

def getInteger(self):
  i, okPressed = QInputDialog.getInt(self, "Get integer","Percentage:", 28, 0, 100, 1)
  if okPressed:
   print(i)

3)double

def getDouble(self):
  d, okPressed = QInputDialog.getDouble(self, "Get double","Value:", 10.05, 0, 100, 10)
  if okPressed:
   print(d)

4)条目

def getChoice(self): #Get item/choice
 items = ("Red","Blue","Green")
 item, okPressed = QInputDialog.getItem(self, "Get item","Color:", items, 0, False)
 if okPressed and item:
  print(item)

简单例子1【引用出处】:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit
from PyQt5.QtGui import QIcon

class App(QWidget):

 def __init__(self):
  super().__init__()
  self.title = 'PyQt5 input dialogs - pythonspot.com'
  self.left = 10
  self.top = 10
  self.width = 640
  self.height = 480
  self.initUI()

 def initUI(self):
  self.setWindowTitle(self.title)
  self.setGeometry(self.left, self.top, self.width, self.height)

  self.getInteger()
  self.getText()
  self.getDouble()
  self.getChoice()

  self.show()

 def getInteger(self):
  i, okPressed = QInputDialog.getInt(self, "Get integer","Percentage:", 28, 0, 100, 1)
  if okPressed:
   print(i)

 def getDouble(self):
  d, okPressed = QInputDialog.getDouble(self, "Get double","Value:", 10.50, 0, 100, 10)
  if okPressed:
   print( d)

 def getChoice(self):
  items = ("Red","Blue","Green")
  item, okPressed = QInputDialog.getItem(self, "Get item","Color:", items, 0, False)
  if ok and item:
   print(item)

 def getText(self):
  text, okPressed = QInputDialog.getText(self, "Get text","Your name:", QLineEdit.Normal, "")
  if okPressed and text != '':
   print(text)

if __name__ == '__main__':
 app = QApplication(sys.argv)
 ex = App()
 sys.exit(app.exec_())

输入对话框使用例子2:

方法一>使用代码创建

————请参考 https://3water.com/article/163860.htm

方法二>使用Qt设计器创建

step1:使用Qt设计器创建GUI,如下图:

对PyQt5的输入对话框使用(QInputDialog)详解

说明:

第4行控件为:出生年月(label)——label_date——dateButton——Date Edit

最右上角的控件为LineEdit框,用于输入日期的,这是一个废弃的控件,没有来得及删除。

图中第二列(用label显示)和第四列(用lineEdit显示)的显示结果一样,且第四列还具有和按钮控件输入功能。

step2:保存为.ui文件,并将其转为myinputdialog.py文件,执行该文件;

myinputdialog.py文件中的类名为:

class Ui_Dialog(object):

def setupUi(self, Dialog):

step3:新建主函数文件为myinputdialogmain.py,在此文件中添加如下代码:

from PyQt5.QtWidgets import *
import sys
from MyInputDialog2 import Ui_Dialog

class MyDialog(QDialog,Ui_Dialog):
 def __init__(self):
  super(MyDialog,self).__init__()
  self.setupUi(self)
  self.setWindowTitle("输入对话框实验")

  #6个按钮
  self.nameButton.clicked.connect(self.inputName)
  self.sexButton.clicked.connect(self.inputSex)
  self.ageButton.clicked.connect(self.inputAge)
  self.dateButton.clicked.connect(self.inputDate2) # Date Edit
  self.dateButton.clicked.connect(self.inputDate1) #对话框
  self.HButton.clicked.connect(self.inputHeight)
  self.WButton.clicked.connect(self.inputWeight)
  #6个Label显示标签
  #label_name,label_sex,label_age,label_date,label_h,label_w
  #7个LineEdit编辑框用于输入信息,与上面按钮具有同样功能
  #namelineEdit,sexlineEdit,agelineEdit,datelineEdit,hlineEdit,wlineEdit,lovelineEdit


 def inputName(self):
  name2 = self.namelineEdit.text()
  name, ok = QInputDialog.getText(self, "用户名",
          "请输入新的名字:",
          QLineEdit.Normal, self.label_name.text())
  if ok:
   self.label_name.setText(name)
   self.namelineEdit.setText(name)
  else:
   self.label_name.setText(name2)

 def inputSex(self):
  list = []
  list.append("男")
  list.append("女")
  sex, ok = QInputDialog.getItem(self, "性别", "请选择性别", list)
  if ok:
   self.label_sex.setText(sex)
   self.sexlineEdit.setText(sex)

 def inputAge(self):
  age, ok = QInputDialog.getInt(self, "年龄","请输入年龄:",
          int(self.label_age.text()), 0, 150,4)

  if ok:
   self.label_age.setText(str(age))
   self.agelineEdit.setText(str(age))
 def inputDate1(self):
  dd, ok = QInputDialog.getText(self, "出生年月",
          "请输入新的出生年月:",
          QLineEdit.Normal, self.label_date.text())
  if ok:
   self.label_date.setText(dd)
   self.datelineEdit.setText(dd)
 def inputDate2(self):
  time = self.dateEdit.text()
  self.label_date.setText(str(time))

 def inputHeight(self):
  stature, ok = QInputDialog.getDouble(self, "身高",
            "请输入身高:",
            float(self.label_h.text()), -2300.0000, 2300.9999,4)
  if ok:
   self.label_h.setText(str(stature))
   self.hlineEdit.setText(str(stature))
 def inputWeight(self):
  stature, ok = QInputDialog.getDouble(self, "身高",
            "请输入身高:",
            float(self.label_w.text()), 0, 2300.00,2)
  if ok:
   self.label_w.setText(str(stature))
   self.wlineEdit.setText(str(stature))



if __name__ == "__main__":
 app = QApplication(sys.argv)
 main = MyDialog()
 main.show()
 sys.exit(app.exec_())

以上这篇对PyQt5的输入对话框使用(QInputDialog)详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python计算字符宽度的方法
Jun 14 Python
python3实现读取chrome浏览器cookie
Jun 19 Python
python中闭包Closure函数作为返回值的方法示例
Dec 17 Python
Django基于ORM操作数据库的方法详解
Mar 27 Python
利用python将图片版PDF转文字版PDF
May 03 Python
flask/django 动态查询表结构相同表名不同数据的Model实现方法
Aug 29 Python
pytorch使用 to 进行类型转换方式
Jan 08 Python
使用python matplotlib 画图导入到word中如何保证分辨率
Apr 16 Python
在spyder IPython console中,运行代码加入参数的实例
Apr 20 Python
Python map及filter函数使用方法解析
Aug 06 Python
Lombok插件安装(IDEA)及配置jar包使用详解
Nov 04 Python
python实现文件+参数发送request的实例代码
Jan 05 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
Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
Jun 25 #Python
You might like
DC《神奇女侠2》因疫情推迟上映 温子仁新恐怖片《恶性》撤档
2020/04/09 欧美动漫
Linux下PHP连接Oracle数据库
2014/08/20 PHP
PHP永久登录、记住我功能实现方法和安全做法
2015/04/27 PHP
jQuery学习笔记之DOM对象和jQuery对象
2010/12/22 Javascript
JavaScript NaN和Infinity特殊值 [译]
2012/09/20 Javascript
jquery阻止冒泡事件使用模拟事件
2013/09/06 Javascript
jquery mobile changepage的三种传参方法介绍
2013/09/13 Javascript
document.compatMode的CSS1compat使用介绍
2014/04/03 Javascript
JavaScript实现关键字高亮功能
2014/11/12 Javascript
jquery ztree实现树的搜索功能
2016/02/25 Javascript
EditPlus 正则表达式 实战(3)
2016/12/15 Javascript
JavaScript获取短信验证码(周期性)
2016/12/29 Javascript
使用cropper.js裁剪头像的实例代码
2017/09/29 Javascript
jQuery实现鼠标移到某个对象时弹出显示层功能
2018/08/23 jQuery
在vue中实现禁止回退上一步,路由不存历史记录
2020/07/22 Javascript
Vue循环中多个input绑定指定v-model实例
2020/08/31 Javascript
[09:34]2018DOTA2国际邀请赛寻真——永不放弃的iG
2018/08/14 DOTA
Python简单删除目录下文件以及文件夹的方法
2015/05/27 Python
Python读写ini文件的方法
2015/05/28 Python
Python实现查找系统盘中需要找的字符
2015/07/14 Python
初步认识Python中的列表与位运算符
2015/10/12 Python
Python元组常见操作示例
2019/02/19 Python
8种用Python实现线性回归的方法对比详解
2019/07/10 Python
python创建ArcGIS shape文件的实现
2019/12/06 Python
Html5新增标签有哪些
2017/04/13 HTML / CSS
新浪网技术部笔试题
2016/08/26 面试题
异步传递消息系统的作用
2016/05/01 面试题
在校生汽车维修实习自我鉴定
2013/09/19 职场文书
学生自我评价范文
2014/02/02 职场文书
预备党员政审材料
2014/02/04 职场文书
高一化学教学反思
2014/02/05 职场文书
艺术学院毕业生自我评价
2014/03/02 职场文书
大学生村官座谈会发言材料
2014/05/25 职场文书
防灾减灾活动总结
2014/08/30 职场文书
企业办公室主任岗位职责
2015/04/01 职场文书
pytest配置文件pytest.ini的详细使用
2021/04/17 Python