欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2019年06月25日 10:07:05   作者:追逐陽(yáng)光的風(fēng)  
今天小編就為大家分享一篇Pyqt5 基本界面組件之inputDialog的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

QInputDialog類提供了一種簡(jiǎn)單方面的對(duì)話框來獲得用戶的單個(gè)輸入信息,可以是一個(gè)字符串,一個(gè)Int類型數(shù)據(jù),一個(gè)double類型數(shù)據(jù)或是一個(gè)下拉列表框的條目。

對(duì)應(yīng)的Dialog其中包括一個(gè)提示標(biāo)簽,一個(gè)輸入控件(若是調(diào)用字符串輸入框,則為一個(gè)QLineEdit,若是調(diào)用Int類型或double類型,則為一個(gè)QSpinBox,若是調(diào)用列表?xiàng)l目輸入框,則為一個(gè)QComboBox),還包括一個(gè)確定輸入(Ok)按鈕和一個(gè)取消輸入(Cancel)按鈕。

QInputDialog:

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

QInputDialog同樣繼承自QDialog,提供簡(jiǎn)單輸入的對(duì)話框,

代碼示例 :

示例代碼如下:

#-*- 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("項(xiàng)目信息")
    self.setGeometry(400,400,300,260)

    label1=QLabel("項(xiàng)目名稱:")
    label2=QLabel("項(xiàng)目類型:")
    label3=QLabel("項(xiàng)目人員:")
    label4=QLabel("項(xiàng)目成本:")
    label5=QLabel("項(xiàng)目介紹:")

    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,"項(xiàng)目名稱","輸入項(xiàng)目名稱:",
                    QLineEdit.Normal,self.nameLable.text())
    if ok and (len(name)!=0):
      self.nameLable.setText(name)
  def selectStyle(self):
    list = ["外包","自研"]

    style,ok = QInputDialog.getItem(self,"項(xiàng)目性質(zhì)","請(qǐng)選擇項(xiàng)目性質(zhì):",list)
    if ok :
      self.styleLable.setText(style)

  def selectNumber(self):
    number,ok = QInputDialog.getInt(self,"項(xiàng)目成員","請(qǐng)輸入項(xiàng)目成員人數(shù):",int(self.numberLable.text()),20,100,2)
    if ok :
      self.numberLable.setText(str(number))

  def selectCost(self):
    cost,ok = QInputDialog.getDouble(self,"項(xiàng)目成本","請(qǐng)輸入項(xiàng)目成員人數(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,"項(xiàng)目介紹","介紹:","服務(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)擊不同的按鈕,來選擇不同類型的輸入對(duì)話框,從而選擇所需的數(shù)據(jù)。

代碼分析:

L18~22:

    label1=QLabel("項(xiàng)目名稱:")
    label2=QLabel("項(xiàng)目類型:")
    label3=QLabel("項(xiàng)目人員:")
    label4=QLabel("項(xiàng)目成本:")
    label5=QLabel("項(xiàng)目介紹:")

定義了數(shù)據(jù)項(xiàng)名稱的標(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)

定義了項(xiàng)目數(shù)據(jù)項(xiàng)中的數(shù)據(jù)內(nèi)容,數(shù)據(jù)內(nèi)容顯示在對(duì)應(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對(duì)象,并將對(duì)應(yīng)的clicked信號(hào)和自定義的槽函數(shù)綁定起來。

L46~61:

實(shí)例化網(wǎng)格布局,并將對(duì)應(yīng)的控件添加到網(wǎng)格布局中。

功能分析:

1:獲取項(xiàng)目名稱:

  def selectName(self):
    name,ok = QInputDialog.getText(self,"項(xiàng)目名稱","輸入項(xiàng)目名稱:", 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)字符串輸入對(duì)話框,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個(gè)參數(shù)parent,用于指定父組件;

第2個(gè)參數(shù)str,是標(biāo)準(zhǔn)輸入對(duì)話框的標(biāo)題名;

第3個(gè)參數(shù)str,標(biāo)準(zhǔn)輸入對(duì)話框的標(biāo)簽提示;

第4個(gè)參數(shù)echo,mode指定標(biāo)準(zhǔn)輸入對(duì)話框中QLineEdit控件的輸入模式;

第5個(gè)參數(shù)str,標(biāo)準(zhǔn)輸入對(duì)話框中QLineEdit控件的默認(rèn)值;

第6個(gè)參數(shù)flags,指明標(biāo)準(zhǔn)輸入對(duì)話框的窗體標(biāo)識(shí);

第7個(gè)參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局;

單擊nameButton之后的效果:

若用戶單擊了“OK”按鈕,則把新輸入的名稱更新至顯示標(biāo)簽。

2:獲取項(xiàng)目屬性:

  def selectStyle(self):
    list = ["外包","自研"]
    style,ok = QInputDialog.getItem(self,"項(xiàng)目性質(zhì)","請(qǐng)選擇項(xiàng)目性質(zhì):",list)
    if ok :
      self.styleLable.setText(style)

調(diào)用QInputDialog的getItem()函數(shù)彈出標(biāo)準(zhǔn)條目選擇對(duì)話框,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個(gè)參數(shù)parent,用于指定父組件;

第2個(gè)參數(shù)str,是標(biāo)準(zhǔn)條目選擇對(duì)話框的標(biāo)題名;

第3個(gè)參數(shù)str,標(biāo)準(zhǔn)條目選擇對(duì)話框的標(biāo)簽提示;

第4個(gè)參數(shù)list-of-str,標(biāo)準(zhǔn)條目選擇對(duì)話框中對(duì)應(yīng)條目的list;

第5個(gè)參數(shù)editable,標(biāo)準(zhǔn)條目選擇對(duì)話框條目是否可編輯標(biāo)識(shí),默認(rèn)為不可編輯;

第6個(gè)參數(shù)flags,指明標(biāo)準(zhǔn)輸入對(duì)話框的窗體標(biāo)識(shí);

第7個(gè)參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局.;

單擊styleButton之后的效果:

若用戶單擊了“OK”按鈕,則把新選擇的類型更新至顯示標(biāo)簽。

3:獲取項(xiàng)目成員:

  def selectNumber(self):
    number,ok = QInputDialog.getInt(self,"項(xiàng)目成員","請(qǐng)輸入項(xiàng)目成員人數(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類型輸入對(duì)話框,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個(gè)參數(shù)parent,用于指定父組件;

第2個(gè)參數(shù)str,是標(biāo)準(zhǔn)int類型輸入對(duì)話框的標(biāo)題名;

第3個(gè)參數(shù)str,標(biāo)準(zhǔn)int類型輸入對(duì)話框的標(biāo)簽提示;

第4個(gè)參數(shù)value,標(biāo)準(zhǔn)int類型輸入對(duì)話框中的默認(rèn)值;

第5個(gè)參數(shù)min,標(biāo)準(zhǔn)int類型輸入對(duì)話框中的最小值;

第6個(gè)參數(shù)max,標(biāo)準(zhǔn)int類型輸入對(duì)話框中的最大值;

第7個(gè)參數(shù)step,標(biāo)準(zhǔn)int類型輸入對(duì)話框中的步長(zhǎng),即QSpinBox中上下選擇是數(shù)據(jù)變化的步長(zhǎng);

第8個(gè)參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局;

單擊numberButton之后的效果:

若用戶單擊了“OK”按鈕,則把新選擇的成員數(shù)據(jù)更新至顯示標(biāo)簽。

4:獲取項(xiàng)目成本:

  def selectCost(self):
    cost,ok = QInputDialog.getDouble(self,"項(xiàng)目成本","請(qǐng)輸入項(xiàng)目成員人數(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類型輸入對(duì)話框,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個(gè)參數(shù)parent,用于指定父組件;

第2個(gè)參數(shù)str,輸入對(duì)話框的標(biāo)題名;

第3個(gè)參數(shù)str,輸入對(duì)話框的標(biāo)簽提示;

第4個(gè)參數(shù)value,標(biāo)準(zhǔn)float類型輸入對(duì)話框中的默認(rèn)值;

第5個(gè)參數(shù)min,標(biāo)準(zhǔn)float類型輸入對(duì)話框中的最小值;

第6個(gè)參數(shù)max,標(biāo)準(zhǔn)float類型輸入對(duì)話框中的最大值;

第7個(gè)參數(shù)decimals,小數(shù)點(diǎn)后面保留的位數(shù);

第8個(gè)參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局;

單擊costButton之后的效果:

若用戶單擊了“OK”按鈕,則把新選擇的成本數(shù)據(jù)更新至顯示標(biāo)簽

5:獲取項(xiàng)目介紹:

  def selectIntroduction(self):
    introduction,ok = QInputDialog.getMultiLineText(self,"項(xiàng)目介紹","介紹:","服務(wù)外包第三方公司 \nPython project")
    if ok :
      self.introductionLable.setText(introduction)

調(diào)用QInputDialog的getMultiLineText()函數(shù)彈出標(biāo)準(zhǔn)多行文本類型輸入對(duì)話框,getMultiLineText()函數(shù)原型如下:

 | getMultiLineText(...)
 |   QInputDialog.getMultiLineText(QWidget, str, str, str text='', Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) -> (str, bool)

第1個(gè)參數(shù)parent,用于指定父組件;

第2個(gè)參數(shù)str,輸入對(duì)話框的標(biāo)題名;

第3個(gè)參數(shù)str,輸入對(duì)話框的標(biāo)簽提示;

第4個(gè)參數(shù)text,輸入對(duì)話框中LineEdit的默認(rèn)值;

第5個(gè)參數(shù)flags,指明標(biāo)準(zhǔn)輸入對(duì)話框的窗體標(biāo)識(shí);

第6個(gè)參數(shù)inputMethodHints,通過選擇不同的inputMethodHints值來實(shí)現(xiàn)不同的鍵盤布局;

單擊introductionButton之后的效果:

若用戶單擊了“OK”按鈕,則把新修改的項(xiàng)目介紹信息更新至顯示標(biāo)簽

以上這篇Pyqt5 基本界面組件之inputDialog的使用就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論