對PyQt5的輸入對話框使用(QInputDialog)詳解
PyQt5中QInputDialog的使用,Qt的QInputDialog類提供了一種簡單方面的對話框來獲得用戶的單個輸入信息,它提供了4種數(shù)據(jù)類型的輸入:
1)字符串型(方法=QInputDialog.getText);
2)Int類型數(shù)據(jù)(方法=QInputDialog.getInt);
3)double類型數(shù)據(jù)(方法=QInputDialog.getDouble);
4)下拉列表框的條目(方法=QInputDialog.getItem)。
QInputDialog繼承自QDialog,提供簡單輸入的對話框:
class QInputDialog(QDialog)
| QInputDialog(QWidget parent=None, Qt.WindowFlags flags=0)
QInputDialog簡介:
Qt提供了一個QInputDialog類,QInputDialogDialog類提供了一種簡單方便的對話框來獲得用戶的單個輸入信息,目前提供了4種數(shù)據(jù)類型的輸入,可以使一個字符串、一個Int類型數(shù)據(jù)、一個double類型數(shù)據(jù)或者是一個下拉列表框的條目。一個標(biāo)準(zhǔn)輸入對話框的基本結(jié)構(gòu)如下圖所示:
其中包含一個提示標(biāo)簽,一個輸入控件。如實調(diào)用字符串輸入框,則為一個QLineEdit;若是調(diào)用Int類型或都報了類型輸入框,則為一個QSpinBox;若是調(diào)用列表條目輸入框,則為一個QComboBox;還包括一個確定輸入(OK)按鈕和一個取消輸入(Cancel)按鈕。
QInputDialog的靜態(tài)函數(shù)
1、getText()
QInputDialog的getText()函數(shù)彈出標(biāo)準(zhǔn)字符串輸入對話框,getText()函數(shù)原型如下:
QString getText( QWidget * parent, #標(biāo)準(zhǔn)輸入對話框的父窗口
const QString & title, #輸入對話框的標(biāo)題名
const QString & label,#標(biāo)準(zhǔn)輸入對話框的標(biāo)簽提示
const QString & text = QString(), #標(biāo)準(zhǔn)字符串輸入對話框彈出時QLineEdit控件中默認出現(xiàn)的文字
bool * ok = 0, #用于指示標(biāo)準(zhǔn)輸入對話框的哪個按鈕被觸發(fā),若ok為true,則表示用戶單擊了OK(確定)按鈕,若ok為false,則表示用戶單擊了Cancel(取消)按鈕
Qt::WindowFlags flags = 0, #知名標(biāo)準(zhǔn)輸入對話框的窗體標(biāo)識
Qt::InputMethodHints inputMethodHints = Qt::ImhNone ); [static]
2、getItem()
QInputDialog的getItem()函數(shù)彈出標(biāo)準(zhǔn)條目選擇對話框,getItem()函數(shù)原型如下:
QString getItem( QWidget * parent, 標(biāo)準(zhǔn)輸入對話框的父窗口
const QString & title, 標(biāo)準(zhǔn)輸入對話框的標(biāo)題名
const QString & label, 標(biāo)準(zhǔn)輸入對話框的標(biāo)簽提示
const QStringList & list, 指定標(biāo)準(zhǔn)輸入對話框中QComboBox控件顯示的可選條目,為一個QStringList對象
int current = 0, 指定標(biāo)準(zhǔn)輸入對話框中QComboBox控件顯示的可選條目,為一個QStringList對象
bool editable = true, 指定QComboBox控件中顯示的文字是否可編輯;
bool * ok = 0, 用于指定標(biāo)準(zhǔn)輸入對話框的哪個那妞被觸發(fā),若ok為false,則表示用戶單擊了Cancel(取消)按鈕;
Qt::WindowFlags f = 0 ) ; [static]用于指定標(biāo)準(zhǔn)輸入對話框的哪個那妞被觸發(fā),若ok為false,則表示用戶單擊了Cancel(取消)按鈕;
3、getInteger()
QInputDialog的getInteger()函數(shù)彈出標(biāo)準(zhǔn)int類型輸入對話框,getInteger()函數(shù)原型如下:
int getInteger( QWidget * parent, 父窗口
const QString & title,標(biāo)題名
const QString & label, 標(biāo)簽提示
int value = 0, 指定標(biāo)準(zhǔn)輸入對話框中QSpinBox控件默認顯示值
int minValue = -2147483647,
int maxValue = 2147483647, 指定QSpinBoxBox控件的數(shù)值范圍,最小和最大值
int step = 1, step指定QSpinBox控件的步進值(即步長)
bool * ok = 0,
Qt::WindowFlags f = 0 ) ;
4、getDouble()
QInputDialog的getDouble()函數(shù)彈出標(biāo)準(zhǔn)double類型輸入對話框,getDouble()函數(shù)原型如下:
double getDouble( QWidget * parent,
const QString & title,
const QString & label,標(biāo)簽提示
double value = 0, 指定標(biāo)準(zhǔn)輸入對話框中QSpinBox控件默認顯示值
double minValue = -2147483647,
double maxValue 2147483647,
int decimals = 1, 指定QSpinBox控件的浮動數(shù)的小數(shù)點位數(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)
簡單例子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:
方法一>使用代碼創(chuàng)建
————請參考 http://www.dbjr.com.cn/article/163860.htm
方法二>使用Qt設(shè)計器創(chuàng)建
step1:使用Qt設(shè)計器創(chuàng)建GUI,如下圖:

說明:
第4行控件為:出生年月(label)——label_date——dateButton——Date Edit
最右上角的控件為LineEdit框,用于輸入日期的,這是一個廢棄的控件,沒有來得及刪除。
圖中第二列(用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("輸入對話框?qū)嶒?)
#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顯示標(biāo)簽
#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)詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- python GUI庫圖形界面開發(fā)之PyQt5打開保存對話框QFileDialog詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5輸入對話框QInputDialog詳細使用方法與實例
- PyQt5基本控件使用之消息彈出、用戶輸入、文件對話框的使用方法
- Python 使用PyQt5 完成選擇文件或目錄的對話框方法
- PyQt5 窗口切換與自定義對話框的實例
- python3+PyQt5+Qt Designer實現(xiàn)擴展對話框
- PyQt5打開文件對話框QFileDialog實例代碼
- Python PyQt5標(biāo)準(zhǔn)對話框用法示例
- Python之PyQt6對話框的實現(xiàn)
相關(guān)文章
Python項目實戰(zhàn)之使用Django框架實現(xiàn)支付寶付款功能
這篇文章主要介紹了Python項目實戰(zhàn)之使用Django框架實現(xiàn)支付寶付款功能,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
使用numpy實現(xiàn)矩陣的翻轉(zhuǎn)(flip)與旋轉(zhuǎn)
這篇文章主要介紹了使用numpy實現(xiàn)矩陣的翻轉(zhuǎn)(flip)與旋轉(zhuǎn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
python中json.dumps()和json.loads()的用法
json.dumps()和json.loads()?json.dumps()用于將字典形式轉(zhuǎn)換為字符串,下面這篇文章主要給大家介紹了關(guān)于python中json.dumps()和json.loads()用法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09

