溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在PyQt5中使用QInputDialog輸入對話框

發(fā)布時間:2021-03-24 17:13:23 來源:億速云 閱讀:214 作者:Leah 欄目:開發(fā)技術

今天就跟大家聊聊有關如何在PyQt5中使用QInputDialog輸入對話框,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

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的靜態(tài)函數

1、getText()

QInputDialog的getText()函數彈出標準字符串輸入對話框,getText()函數原型如下:

QString getText( QWidget * parent, #標準輸入對話框的父窗口

const QString & title, #輸入對話框的標題名

const QString & label,#標準輸入對話框的標簽提示

const QString & text = QString(), #標準字符串輸入對話框彈出時QLineEdit控件中默認出現的文字

bool * ok = 0, #用于指示標準輸入對話框的哪個按鈕被觸發(fā),若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, 用于指定標準輸入對話框的哪個那妞被觸發(fā),若ok為false,則表示用戶單擊了Cancel(取消)按鈕;

Qt::WindowFlags f = 0 ) ; [static]用于指定標準輸入對話框的哪個那妞被觸發(fā),若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:

方法一>使用代碼創(chuàng)建

————請參考 https://www.jb51.net/article/163860.htm

方法二>使用Qt設計器創(chuàng)建

step1:使用Qt設計器創(chuàng)建GUI,如下圖:

如何在PyQt5中使用QInputDialog輸入對話框

說明:

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

最右上角的控件為LineEdit框,用于輸入日期的,這是一個廢棄的控件,沒有來得及刪除。

圖中第二列(用label顯示)和第四列(用lineEdit顯示)的顯示結果一樣,且第四列還具有和按鈕控件輸入功能。

step2:保存為.ui文件,并將其轉為myinputdialog.py文件,執(zhí)行該文件;

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輸入對話框有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI