Pyqt5 基本界面組件之inputDialog的使用
QInputDialog類提供了一種簡單方面的對話框來獲得用戶的單個輸入信息,可以是一個字符串,一個Int類型數(shù)據(jù),一個double類型數(shù)據(jù)或是一個下拉列表框的條目。
對應(yīng)的Dialog其中包括一個提示標(biāo)簽,一個輸入控件(若是調(diào)用字符串輸入框,則為一個QLineEdit,若是調(diào)用Int類型或double類型,則為一個QSpinBox,若是調(diào)用列表條目輸入框,則為一個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("服務(wù)外包第三方公司")
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,"項目性質(zhì)","請選擇項目性質(zhì):",list)
if ok :
self.styleLable.setText(style)
def selectNumber(self):
number,ok = QInputDialog.getInt(self,"項目成員","請輸入項目成員人數(shù):",int(self.numberLable.text()),20,100,2)
if ok :
self.numberLable.setText(str(number))
def selectCost(self):
cost,ok = QInputDialog.getDouble(self,"項目成本","請輸入項目成員人數(shù):",float(self.costLable.text()),100.00,500.00,2)
if ok :
self.costLable.setText(str(cost))
def selectIntroduction(self):
introduction,ok = QInputDialog.getMultiLineText(self,"項目介紹","介紹:","服務(wù)外包第三方公司 \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_())
運(yùn)行之后的效果:

示例說明:
通過點(diǎn)擊不同的按鈕,來選擇不同類型的輸入對話框,從而選擇所需的數(shù)據(jù)。
代碼分析:
L18~22:
label1=QLabel("項目名稱:")
label2=QLabel("項目類型:")
label3=QLabel("項目人員:")
label4=QLabel("項目成本:")
label5=QLabel("項目介紹:")
定義了數(shù)據(jù)項名稱的標(biāo)簽。
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("服務(wù)外包第三方公司")
self.introductionLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
定義了項目數(shù)據(jù)項中的數(shù)據(jù)內(nèi)容,數(shù)據(jù)內(nèi)容顯示在對應(yīng)的標(biāo)簽中。
setFrameStyle()設(shè)定標(biāo)簽的樣式,有如下的樣式:
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)
實(shí)例化QPushButton對象,并將對應(yīng)的clicked信號和自定義的槽函數(shù)綁定起來。
L46~61:
實(shí)例化網(wǎng)格布局,并將對應(yīng)的控件添加到網(wǎng)格布局中。
功能分析:
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中很多方法均為靜態(tài)方法,因此不需要實(shí)例化直接可以調(diào)用。調(diào)用QInputDialog的getText()函數(shù)彈出標(biāo)準(zhǔn)字符串輸入對話框,getText()函數(shù)原型如下:
| 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個參數(shù)parent,用于指定父組件;
第2個參數(shù)str,是標(biāo)準(zhǔn)輸入對話框的標(biāo)題名;
第3個參數(shù)str,標(biāo)準(zhǔn)輸入對話框的標(biāo)簽提示;
第4個參數(shù)echo,mode指定標(biāo)準(zhǔn)輸入對話框中QLineEdit控件的輸入模式;
第5個參數(shù)str,標(biāo)準(zhǔn)輸入對話框中QLineEdit控件的默認(rèn)值;
第6個參數(shù)flags,指明標(biāo)準(zhǔn)輸入對話框的窗體標(biāo)識;
第7個參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局;
單擊nameButton之后的效果:

若用戶單擊了“OK”按鈕,則把新輸入的名稱更新至顯示標(biāo)簽。
2:獲取項目屬性:
def selectStyle(self):
list = ["外包","自研"]
style,ok = QInputDialog.getItem(self,"項目性質(zhì)","請選擇項目性質(zhì):",list)
if ok :
self.styleLable.setText(style)
調(diào)用QInputDialog的getItem()函數(shù)彈出標(biāo)準(zhǔn)條目選擇對話框,getItem()函數(shù)原型如下:
| 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個參數(shù)parent,用于指定父組件;
第2個參數(shù)str,是標(biāo)準(zhǔn)條目選擇對話框的標(biāo)題名;
第3個參數(shù)str,標(biāo)準(zhǔn)條目選擇對話框的標(biāo)簽提示;
第4個參數(shù)list-of-str,標(biāo)準(zhǔn)條目選擇對話框中對應(yīng)條目的list;
第5個參數(shù)editable,標(biāo)準(zhǔn)條目選擇對話框條目是否可編輯標(biāo)識,默認(rèn)為不可編輯;
第6個參數(shù)flags,指明標(biāo)準(zhǔn)輸入對話框的窗體標(biāo)識;
第7個參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局.;
單擊styleButton之后的效果:

若用戶單擊了“OK”按鈕,則把新選擇的類型更新至顯示標(biāo)簽。
3:獲取項目成員:
def selectNumber(self):
number,ok = QInputDialog.getInt(self,"項目成員","請輸入項目成員人數(shù):",int(self.numberLable.text()),20,100,2)
if ok :
self.numberLable.setText(str(number))
調(diào)用QInputDialog的getInt()函數(shù)彈出標(biāo)準(zhǔn)int類型輸入對話框,getInt()函數(shù)原型如下:
| 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個參數(shù)parent,用于指定父組件;
第2個參數(shù)str,是標(biāo)準(zhǔn)int類型輸入對話框的標(biāo)題名;
第3個參數(shù)str,標(biāo)準(zhǔn)int類型輸入對話框的標(biāo)簽提示;
第4個參數(shù)value,標(biāo)準(zhǔn)int類型輸入對話框中的默認(rèn)值;
第5個參數(shù)min,標(biāo)準(zhǔn)int類型輸入對話框中的最小值;
第6個參數(shù)max,標(biāo)準(zhǔn)int類型輸入對話框中的最大值;
第7個參數(shù)step,標(biāo)準(zhǔn)int類型輸入對話框中的步長,即QSpinBox中上下選擇是數(shù)據(jù)變化的步長;
第8個參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局;
單擊numberButton之后的效果:

若用戶單擊了“OK”按鈕,則把新選擇的成員數(shù)據(jù)更新至顯示標(biāo)簽。
4:獲取項目成本:
def selectCost(self):
cost,ok = QInputDialog.getDouble(self,"項目成本","請輸入項目成員人數(shù):",float(self.costLable.text()),100.00,500.00,2)
if ok :
self.costLable.setText(str(cost))
調(diào)用QInputDialog的getDouble()函數(shù)彈出標(biāo)準(zhǔn)float類型輸入對話框,getDouble()函數(shù)原型如下:
| 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個參數(shù)parent,用于指定父組件;
第2個參數(shù)str,輸入對話框的標(biāo)題名;
第3個參數(shù)str,輸入對話框的標(biāo)簽提示;
第4個參數(shù)value,標(biāo)準(zhǔn)float類型輸入對話框中的默認(rèn)值;
第5個參數(shù)min,標(biāo)準(zhǔn)float類型輸入對話框中的最小值;
第6個參數(shù)max,標(biāo)準(zhǔn)float類型輸入對話框中的最大值;
第7個參數(shù)decimals,小數(shù)點(diǎn)后面保留的位數(shù);
第8個參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局;
單擊costButton之后的效果:

若用戶單擊了“OK”按鈕,則把新選擇的成本數(shù)據(jù)更新至顯示標(biāo)簽
5:獲取項目介紹:
def selectIntroduction(self):
introduction,ok = QInputDialog.getMultiLineText(self,"項目介紹","介紹:","服務(wù)外包第三方公司 \nPython project")
if ok :
self.introductionLable.setText(introduction)
調(diào)用QInputDialog的getMultiLineText()函數(shù)彈出標(biāo)準(zhǔn)多行文本類型輸入對話框,getMultiLineText()函數(shù)原型如下:
| getMultiLineText(...) | QInputDialog.getMultiLineText(QWidget, str, str, str text='', Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) -> (str, bool)
第1個參數(shù)parent,用于指定父組件;
第2個參數(shù)str,輸入對話框的標(biāo)題名;
第3個參數(shù)str,輸入對話框的標(biāo)簽提示;
第4個參數(shù)text,輸入對話框中LineEdit的默認(rèn)值;
第5個參數(shù)flags,指明標(biāo)準(zhǔn)輸入對話框的窗體標(biāo)識;
第6個參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局;
單擊introductionButton之后的效果:

若用戶單擊了“OK”按鈕,則把新修改的項目介紹信息更新至顯示標(biāo)簽
以上這篇Pyqt5 基本界面組件之inputDialog的使用就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決ToPILImage時出現(xiàn)維度報錯問題pic should be 2/3 d
這篇文章主要介紹了解決ToPILImage時出現(xiàn)維度報錯問題pic should be 2/3 dimensional. Got 4 dimensions.具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
python3.8.3安裝教程及環(huán)境配置的詳細(xì)教程(64-bit)
這篇文章主要介紹了python3.8.3安裝教程及環(huán)境配置的詳細(xì)教程(64-bit),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Python連接Postgres/Mysql/Mongo數(shù)據(jù)庫基本操作大全
在后端應(yīng)用開發(fā)中,經(jīng)常會用到Postgres/Mysql/Mongo這三種數(shù)據(jù)庫的基本操作,今天小編就給大家詳細(xì)介紹Python連接Postgres/Mysql/Mongo數(shù)據(jù)庫基本操作,感興趣的朋友一起看看吧2021-06-06
Python實(shí)現(xiàn)用手機(jī)監(jiān)控遠(yuǎn)程控制電腦的方法
這篇文章主要介紹了Python實(shí)現(xiàn)用手機(jī)監(jiān)控遠(yuǎn)程控制電腦的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
python3+PyQt5 數(shù)據(jù)庫編程--增刪改實(shí)例
今天小編就為大家分享一篇python3+PyQt5 數(shù)據(jù)庫編程--增刪改實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
python自動化測試之從命令行運(yùn)行測試用例with verbosity
這篇文章主要介紹了python自動化測試之從命令行運(yùn)行測試用例with verbosity,是一個較為經(jīng)典的自動化測試實(shí)例,需要的朋友可以參考下2014-09-09

