Input dialog for PyQt5 (QInputDialog)

The use of QInputDialog in PyQt5. The QInputDialog class of Qt provides a simple dialog box to obtain the user's single input information. It provides four data types of input:
1) String type (method = QInputDialog.getText);
2) Int type data (method = QInputDialog.getInt);
3) double type data (method = QInputDialog.getDouble);
4) The entry in the drop-down list box (method = QInputDialog.getItem).

QInputDialog inherits from QDialog and provides a dialog box for simple input:
class QInputDialog(QDialog)
| QInputDialog(QWidget parent=None, Qt.WindowFlags flags=0)

Introduction to QInputDialog:
Qt provides a QInputDialog class. QInputDialogDialog class provides a simple and convenient dialog box to obtain the user's single input information. At present, it provides four data types of input, which can make a string, an Int type data, a double type data or an entry in a drop-down list box. The basic structure of a standard input dialog box is shown in the following figure:

It contains a prompt label and an input control. If the string input box is called truthfully, it is a QLineEdit; If the Int type is called or the type input box is reported, it is a QSpinBox; If it is a call list entry input box, it is a QComboBox; It also includes an OK button and a Cancel button.

Static function of QInputDialog
1,getText()
The getText() function of QInputDialog pops up a standard string input dialog box. The prototype of getText() function is as follows:
Qstring gettext (QWidget * parent, # parent window of standard input dialog box
Const qstring & title, # enter the title name of the dialog box
Const qstring & label, # label prompt of standard input dialog box
Const qstring & text = qstring(), # the default text in the QLineEdit control when the standard string input dialog box pops up
bool * ok = 0, # used to indicate which button in the standard input dialog box is triggered. If ok is true, it means that the user has clicked the OK button. If ok is false, it means that the user has clicked the Cancel button
Qt::WindowFlags flags = 0, # well known form ID of standard input dialog box
Qt::InputMethodHints inputMethodHints = Qt::ImhNone ); [static]
2,getItem()
The getItem() function of QInputDialog pops up a standard item selection dialog box. The prototype of getItem() function is as follows:
Qstring getitem (QWidget * parent, parent window of standard input dialog box
Const qstring & title, the title name of the standard input dialog box
Const qstring & label, label prompt of standard input dialog box
Const QStringList & list, which specifies the optional entries displayed by the QComboBox control in the standard input dialog box as a QStringList object
int current = 0, specifies the optional entries displayed by the QComboBox control in the standard input dialog box as a QStringList object
bool editable = true, specifies whether the text displayed in the QComboBox control is editable;
bool * ok = 0, used to specify which button of the standard input dialog box is triggered. If ok is false, it means that the user has clicked the Cancel button;
QT:: windowflags (F = 0); [static] used to specify which button in the standard input dialog box is triggered. If ok is false, it means that the user clicks the Cancel button;
3,getInteger()
The getInteger() function of QInputDialog pops up the standard int type input dialog box. The prototype of getInteger() function is as follows:
Int getinteger (QWidget * parent, parent window)
Const qstring & title, title name
Const qstring & label, label prompt
int value = 0, specifies the default display value of QSpinBox control in standard input dialog box
int minValue = -2147483647,
int maxValue = 2147483647, specifies the value range, minimum and maximum values of the QSpinBoxBox control
int step = 1, step specifies the step value (i.e. step size) of the QSpinBox control
bool * ok = 0,
Qt::WindowFlags f = 0 ) ;

4,getDouble()
The getDouble() function of QInputDialog pops up the standard double type input dialog box. The prototype of getDouble() function is as follows:
double getDouble( QWidget * parent,
const QString & title,
Const qstring & label, label prompt
double value = 0, specifies the default display value of QSpinBox control in standard input dialog box
double minValue = -2147483647,
double maxValue 2147483647,
int decimals = 1, specifies the number of decimal places of the floating number of the QSpinBox control
bool * ok = 0,
Qt::WindowFlags f = 0 ) ;

example
1) String

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) Entry

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)



Simple example 1 [source]:

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_())



Example 2 of input dialog box:
Method 1 > create with code
————Please refer to https://blog.csdn.net/zhulove86/article/details/52515460
Method 2 > create with Qt Designer
step1: use Qt designer to create GUI, as shown below:

explain:
The controls in line 4 are: date of birth (label) -- label_date -- dateButton -- Date Edit
The control in the top right corner is the LineEdit box, which is used to enter the date. This is an obsolete control and has not been deleted in time.
The display results of the second column (displayed with label) and the fourth column (displayed with lineEdit) in the figure are the same, and the fourth column also has the function of and button control input.

Step 2: save as a. ui file, convert it to myinputdialog.py file, and execute the file;
The class name in myinputdialog.py file is:
class Ui_Dialog(object):
def setupUi(self, Dialog):
step3: the new main function file is myinputdialogmain.py. Add the following code to this file:

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("Input dialog experiment")

        #6 buttons
        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)  #dialog box
        self.HButton.clicked.connect(self.inputHeight)
        self.WButton.clicked.connect(self.inputWeight)
        #6 labels display labels
        #label_name,label_sex,label_age,label_date,label_h,label_w
        #Seven LineEdit edit boxes are used to input information and have the same function as the buttons above
        #namelineEdit,sexlineEdit,agelineEdit,datelineEdit,hlineEdit,wlineEdit,lovelineEdit


    def inputName(self):
        name2 = self.namelineEdit.text()
        name, ok = QInputDialog.getText(self, "user name",
                                        "Please enter a new name:",
                                        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("male")
        list.append("female")
        sex, ok = QInputDialog.getItem(self, "Gender", "Please select gender", list)
        if ok:
            self.label_sex.setText(sex)
            self.sexlineEdit.setText(sex)

    def inputAge(self):
        age, ok = QInputDialog.getInt(self, "Age","Please enter age:",
                                      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, "date of birth",
                                        "Please enter a new date of birth:",
                                        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, "height",
                                             "Please enter your height:",
                                             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, "height",
                                             "Please enter your height:",
                                             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_())




reference material:
[1] Pyqt5 series (V) - inputdialog of basic interface components, https://blog.csdn.net/zhulove86/article/details/52515460
[2] PyQT5 input dialog. https://pythonspot.com/en/pyqt5-input-dialog/
[3] Use of standard general dialog box of QT https://blog.csdn.net/hbyzl/article/details/49721407
[4] pyqt4 original routine – outputs and prints the contents of the text box LineEdit https://blog.csdn.net/graduate_2017/article/details/70917461
--------
Copyright notice: This article is the original article of CSDN blogger "patience, perseverance, clumsiness, victory and cleverness". It follows CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
Original link: https://blog.csdn.net/panrenlong/article/details/79948261

Tags: Python PyQt5

Posted on Thu, 02 Dec 2021 17:47:01 -0500 by eduard77