對(duì)PyQt5的輸入對(duì)話框使用(QInputDialog)詳解
PyQt5中QInputDialog的使用,Qt的QInputDialog類提供了一種簡(jiǎn)單方面的對(duì)話框來獲得用戶的單個(gè)輸入信息,它提供了4種數(shù)據(jù)類型的輸入:
1)字符串型(方法=QInputDialog.getText);
2)Int類型數(shù)據(jù)(方法=QInputDialog.getInt);
3)double類型數(shù)據(jù)(方法=QInputDialog.getDouble);
4)下拉列表框的條目(方法=QInputDialog.getItem)。
QInputDialog繼承自QDialog,提供簡(jiǎn)單輸入的對(duì)話框:
class QInputDialog(QDialog)
| QInputDialog(QWidget parent=None, Qt.WindowFlags flags=0)
QInputDialog簡(jiǎn)介:
Qt提供了一個(gè)QInputDialog類,QInputDialogDialog類提供了一種簡(jiǎn)單方便的對(duì)話框來獲得用戶的單個(gè)輸入信息,目前提供了4種數(shù)據(jù)類型的輸入,可以使一個(gè)字符串、一個(gè)Int類型數(shù)據(jù)、一個(gè)double類型數(shù)據(jù)或者是一個(gè)下拉列表框的條目。一個(gè)標(biāo)準(zhǔn)輸入對(duì)話框的基本結(jié)構(gòu)如下圖所示:
其中包含一個(gè)提示標(biāo)簽,一個(gè)輸入控件。如實(shí)調(diào)用字符串輸入框,則為一個(gè)QLineEdit;若是調(diào)用Int類型或都報(bào)了類型輸入框,則為一個(gè)QSpinBox;若是調(diào)用列表?xiàng)l目輸入框,則為一個(gè)QComboBox;還包括一個(gè)確定輸入(OK)按鈕和一個(gè)取消輸入(Cancel)按鈕。
QInputDialog的靜態(tài)函數(shù)
1、getText()
QInputDialog的getText()函數(shù)彈出標(biāo)準(zhǔn)字符串輸入對(duì)話框,getText()函數(shù)原型如下:
QString getText( QWidget * parent, #標(biāo)準(zhǔn)輸入對(duì)話框的父窗口
const QString & title, #輸入對(duì)話框的標(biāo)題名
const QString & label,#標(biāo)準(zhǔn)輸入對(duì)話框的標(biāo)簽提示
const QString & text = QString(), #標(biāo)準(zhǔn)字符串輸入對(duì)話框彈出時(shí)QLineEdit控件中默認(rèn)出現(xiàn)的文字
bool * ok = 0, #用于指示標(biāo)準(zhǔn)輸入對(duì)話框的哪個(gè)按鈕被觸發(fā),若ok為true,則表示用戶單擊了OK(確定)按鈕,若ok為false,則表示用戶單擊了Cancel(取消)按鈕
Qt::WindowFlags flags = 0, #知名標(biāo)準(zhǔn)輸入對(duì)話框的窗體標(biāo)識(shí)
Qt::InputMethodHints inputMethodHints = Qt::ImhNone ); [static]
2、getItem()
QInputDialog的getItem()函數(shù)彈出標(biāo)準(zhǔn)條目選擇對(duì)話框,getItem()函數(shù)原型如下:
QString getItem( QWidget * parent, 標(biāo)準(zhǔn)輸入對(duì)話框的父窗口
const QString & title, 標(biāo)準(zhǔn)輸入對(duì)話框的標(biāo)題名
const QString & label, 標(biāo)準(zhǔn)輸入對(duì)話框的標(biāo)簽提示
const QStringList & list, 指定標(biāo)準(zhǔn)輸入對(duì)話框中QComboBox控件顯示的可選條目,為一個(gè)QStringList對(duì)象
int current = 0, 指定標(biāo)準(zhǔn)輸入對(duì)話框中QComboBox控件顯示的可選條目,為一個(gè)QStringList對(duì)象
bool editable = true, 指定QComboBox控件中顯示的文字是否可編輯;
bool * ok = 0, 用于指定標(biāo)準(zhǔn)輸入對(duì)話框的哪個(gè)那妞被觸發(fā),若ok為false,則表示用戶單擊了Cancel(取消)按鈕;
Qt::WindowFlags f = 0 ) ; [static]用于指定標(biāo)準(zhǔn)輸入對(duì)話框的哪個(gè)那妞被觸發(fā),若ok為false,則表示用戶單擊了Cancel(取消)按鈕;
3、getInteger()
QInputDialog的getInteger()函數(shù)彈出標(biāo)準(zhǔn)int類型輸入對(duì)話框,getInteger()函數(shù)原型如下:
int getInteger( QWidget * parent, 父窗口
const QString & title,標(biāo)題名
const QString & label, 標(biāo)簽提示
int value = 0, 指定標(biāo)準(zhǔn)輸入對(duì)話框中QSpinBox控件默認(rèn)顯示值
int minValue = -2147483647,
int maxValue = 2147483647, 指定QSpinBoxBox控件的數(shù)值范圍,最小和最大值
int step = 1, step指定QSpinBox控件的步進(jìn)值(即步長(zhǎng))
bool * ok = 0,
Qt::WindowFlags f = 0 ) ;
4、getDouble()
QInputDialog的getDouble()函數(shù)彈出標(biāo)準(zhǔn)double類型輸入對(duì)話框,getDouble()函數(shù)原型如下:
double getDouble( QWidget * parent,
const QString & title,
const QString & label,標(biāo)簽提示
double value = 0, 指定標(biāo)準(zhǔn)輸入對(duì)話框中QSpinBox控件默認(rèn)顯示值
double minValue = -2147483647,
double maxValue 2147483647,
int decimals = 1, 指定QSpinBox控件的浮動(dòng)數(shù)的小數(shù)點(diǎn)位數(shù)
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)
簡(jiǎn)單例子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_())
輸入對(duì)話框使用例子2:
方法一>使用代碼創(chuàng)建
————請(qǐng)參考 http://www.dbjr.com.cn/article/163860.htm
方法二>使用Qt設(shè)計(jì)器創(chuàng)建
step1:使用Qt設(shè)計(jì)器創(chuàng)建GUI,如下圖:
說明:
第4行控件為:出生年月(label)——label_date——dateButton——Date Edit
最右上角的控件為L(zhǎng)ineEdit框,用于輸入日期的,這是一個(gè)廢棄的控件,沒有來得及刪除。
圖中第二列(用label顯示)和第四列(用lineEdit顯示)的顯示結(jié)果一樣,且第四列還具有和按鈕控件輸入功能。
step2:保存為.ui文件,并將其轉(zhuǎn)為myinputdialog.py文件,執(zhí)行該文件;
myinputdialog.py文件中的類名為:
class Ui_Dialog(object):
def setupUi(self, Dialog):
step3:新建主函數(shù)文件為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("輸入對(duì)話框?qū)嶒?yàn)") #6個(gè)按鈕 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) #對(duì)話框 self.HButton.clicked.connect(self.inputHeight) self.WButton.clicked.connect(self.inputWeight) #6個(gè)Label顯示標(biāo)簽 #label_name,label_sex,label_age,label_date,label_h,label_w #7個(gè)LineEdit編輯框用于輸入信息,與上面按鈕具有同樣功能 #namelineEdit,sexlineEdit,agelineEdit,datelineEdit,hlineEdit,wlineEdit,lovelineEdit def inputName(self): name2 = self.namelineEdit.text() name, ok = QInputDialog.getText(self, "用戶名", "請(qǐng)輸入新的名字:", 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, "性別", "請(qǐng)選擇性別", list) if ok: self.label_sex.setText(sex) self.sexlineEdit.setText(sex) def inputAge(self): age, ok = QInputDialog.getInt(self, "年齡","請(qǐng)輸入年齡:", 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, "出生年月", "請(qǐng)輸入新的出生年月:", 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, "身高", "請(qǐng)輸入身高:", 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, "身高", "請(qǐng)輸入身高:", 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_())
以上這篇對(duì)PyQt5的輸入對(duì)話框使用(QInputDialog)詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python GUI庫圖形界面開發(fā)之PyQt5打開保存對(duì)話框QFileDialog詳細(xì)使用方法與實(shí)例
- python GUI庫圖形界面開發(fā)之PyQt5輸入對(duì)話框QInputDialog詳細(xì)使用方法與實(shí)例
- PyQt5基本控件使用之消息彈出、用戶輸入、文件對(duì)話框的使用方法
- Python 使用PyQt5 完成選擇文件或目錄的對(duì)話框方法
- PyQt5 窗口切換與自定義對(duì)話框的實(shí)例
- python3+PyQt5+Qt Designer實(shí)現(xiàn)擴(kuò)展對(duì)話框
- PyQt5打開文件對(duì)話框QFileDialog實(shí)例代碼
- Python PyQt5標(biāo)準(zhǔn)對(duì)話框用法示例
- Python之PyQt6對(duì)話框的實(shí)現(xiàn)
相關(guān)文章
Python3 翻轉(zhuǎn)二叉樹的實(shí)現(xiàn)
這篇文章主要介紹了Python3 翻轉(zhuǎn)二叉樹的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Python中的descriptor描述器簡(jiǎn)明使用指南
descriptor在Python中主要被用來定義方法和屬性,使用起來相當(dāng)具有技巧性,這里我們先從基礎(chǔ)的開始,整理一份Python中的descriptor描述器簡(jiǎn)明使用指南2016-06-06JSON Web Tokens的實(shí)現(xiàn)原理
本文主要介紹了JSON Web Tokens的實(shí)現(xiàn)原理。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-04-04Python項(xiàng)目實(shí)戰(zhàn)之使用Django框架實(shí)現(xiàn)支付寶付款功能
這篇文章主要介紹了Python項(xiàng)目實(shí)戰(zhàn)之使用Django框架實(shí)現(xiàn)支付寶付款功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02基于python實(shí)現(xiàn)簡(jiǎn)單日歷
這篇文章主要為大家詳細(xì)介紹了基于python實(shí)現(xiàn)簡(jiǎn)單日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07使用numpy實(shí)現(xiàn)矩陣的翻轉(zhuǎn)(flip)與旋轉(zhuǎn)
這篇文章主要介紹了使用numpy實(shí)現(xiàn)矩陣的翻轉(zhuǎn)(flip)與旋轉(zhuǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06python中json.dumps()和json.loads()的用法
json.dumps()和json.loads()?json.dumps()用于將字典形式轉(zhuǎn)換為字符串,下面這篇文章主要給大家介紹了關(guān)于python中json.dumps()和json.loads()用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09