Pyqt5 series - inputDialog of basic interface components

The QInputDialog class provides a simple dialog box to obtain the user's single input information, which can be a string, an Int type data, a double type data or an entry in a drop-down list box.
The corresponding Dialog includes a prompt label, an input control (QLineEdit if calling string input box, QSpinBox if calling Int type or double type, QComboBox if calling list entry input box), an OK button and a cancel button.

QInputDialog:

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

QInputDialog also inherits from QDialog and provides a dialog box for simple input,

Code example:

The example code is as follows:

#-*- 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("Project information")
        self.setGeometry(400,400,300,260)

        label1=QLabel("entry name:")
        label2=QLabel("Project type:")
        label3=QLabel("Project personnel:")
        label4=QLabel("project cost :")
        label5=QLabel("Project introduction:")

        self.nameLable = QLabel("PyQt5")
        self.nameLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
        self.styleLable = QLabel("outsource")
        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("Service outsourcing third party company")
        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,"entry name","Enter project name:",
                                       QLineEdit.Normal,self.nameLable.text())
        if ok and (len(name)!=0):
            self.nameLable.setText(name)
    def selectStyle(self):
        list = ["outsource","Self research"]

        style,ok = QInputDialog.getItem(self,"Project nature","Please select the project nature:",list)
        if ok :
            self.styleLable.setText(style)

    def selectNumber(self):
        number,ok = QInputDialog.getInt(self,"Project members","Please enter the number of project members:",int(self.numberLable.text()),20,100,2)
        if ok :
            self.numberLable.setText(str(number))

    def selectCost(self):
        cost,ok = QInputDialog.getDouble(self,"project cost ","Please enter the number of project members:",float(self.costLable.text()),100.00,500.00,2)
        if ok :
            self.costLable.setText(str(cost))

    def selectIntroduction(self):
        introduction,ok = QInputDialog.getMultiLineText(self,"Project introduction","Introduction:","Service outsourcing third party company \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_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

Effect after operation:

Example description:
Select different types of input dialog boxes by clicking different buttons, so as to select the required data.
Code analysis:
L18~22:

        label1=QLabel("entry name:")
        label2=QLabel("Project type:")
        label3=QLabel("Project personnel:")
        label4=QLabel("project cost :")
        label5=QLabel("Project introduction:")
  • 1
  • 2
  • 3
  • 4
  • 5

Label that defines the name of the data item.

L24~33:

        self.nameLable = QLabel("PyQt5")
        self.nameLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
        self.styleLable = QLabel("outsource")
        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("Third party outsourcing company")
        self.introductionLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Defines the data content in the project data item, and the data content is displayed in the corresponding label.
setFrameStyle() sets the style of the label, including the following styles:
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Instantiate the QPushButton object and bind the corresponding clicked signal with the custom slot function.

L46~61:
Instantiate the grid layout and add the corresponding control to the grid layout.

Functional analysis:

1: Get project name:

    def selectName(self):
        name,ok = QInputDialog.getText(self,"entry name","Enter project name:",                                     QLineEdit.Normal,self.nameLable.text())
        if ok and (len(name)!=0):
            self.nameLable.setText(name)
  • 1
  • 2
  • 3
  • 4

Many methods in QInputDialog are static methods, so they can be called directly without instantiation. Call the getText() function of QInputDialog to pop up the standard string input dialog box. The prototype of getText() function is as follows:

 |  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
  • 2

The first parameter, parent, is used to specify the parent component;

The second parameter str is the title name of the standard input dialog box;

The third parameter str is the label prompt of the standard input dialog box;

The fourth parameter echo, mode specifies the input mode of QLineEdit control in the standard input dialog box;

The fifth parameter str is the default value of QLineEdit control in the standard input dialog box;

The sixth parameter flags indicates the form identification of the standard input dialog box;

The seventh parameter, inputMethodHints, realizes different keyboard layouts by selecting different inputMethodHints values;

Effect after clicking nameButton:

If the user clicks the "OK" button, the newly entered name is updated to the display label.

2: Get project properties:

    def selectStyle(self):
        list = ["outsource","Self research"]
        style,ok = QInputDialog.getItem(self,"Project nature","Please select the project nature:",list)
        if ok :
            self.styleLable.setText(style)
  • 1
  • 2
  • 3
  • 4
  • 5

Call the getItem() function of QInputDialog to pop up the standard item selection dialog box. The prototype of getItem() function is as follows:

 |  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
  • 2

The first parameter, parent, is used to specify the parent component;

The second parameter str is the title name of the standard entry selection dialog box;

The third parameter str is the label prompt of the standard entry selection dialog box;

The fourth parameter, list of STR, is the list of corresponding items in the standard item selection dialog box;

The fifth parameter is editable, which indicates whether the entry in the standard entry selection dialog box is editable. It is non editable by default;

The sixth parameter flags indicates the form identification of the standard input dialog box;

The seventh parameter, inputMethodHints, realizes different keyboard layouts by selecting different inputMethodHints values;

Effect after clicking styleButton:

If the user clicks the "OK" button, the newly selected type is updated to the display label.

3: Get project members:

    def selectNumber(self):
        number,ok = QInputDialog.getInt(self,"Project members","Please enter the number of project members:",int(self.numberLable.text()),20,100,2)
        if ok :
            self.numberLable.setText(str(number))
  • 1
  • 2
  • 3
  • 4

Call the getInt() function of QInputDialog to pop up the standard int type input dialog box. The prototype of getInt() function is as follows:

|  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
  • 2

The first parameter, parent, is used to specify the parent component;

The second parameter str is the title name of the standard int type input dialog box;

The third parameter str is the label prompt of the standard int type input dialog box;

The fourth parameter value is the default value in the standard int type input dialog box;

The fifth parameter min is the minimum value in the standard int type input dialog box;

The 6th parameter max, the maximum value in the standard int type input dialog box;

The seventh parameter step is the step size in the standard int type input dialog box, that is, the up and down selection in QSpinBox is the step size of data change;

The eighth parameter, inputMethodHints, realizes different keyboard layouts by selecting different inputMethodHints values;

Effect after clicking numberButton:

If the user clicks the "OK" button, the newly selected member data will be updated to the display label.

4: Get project cost:

    def selectCost(self):
        cost,ok = QInputDialog.getDouble(self,"project cost ","Please enter the number of project members:",float(self.costLable.text()),100.00,500.00,2)
        if ok :
            self.costLable.setText(str(cost))
  • 1
  • 2
  • 3
  • 4

Call the getDouble() function of QInputDialog to pop up the standard float type input dialog box. The prototype of getDouble() function is as follows:

 |  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
  • 2

The first parameter, parent, is used to specify the parent component;

The second parameter str, enter the title name of the dialog box;

The third parameter str is the label prompt of the input dialog box;

The fourth parameter value is the default value in the standard float type input dialog box;

The fifth parameter min is the minimum value in the standard float type input dialog box;

The sixth parameter max is the maximum value in the standard float type input dialog box;

The seventh parameter decimals is the number of digits reserved after the decimal point;

The eighth parameter, inputMethodHints, realizes different keyboard layouts by selecting different inputMethodHints values;

Effect after clicking costButton:

If the user clicks the "OK" button, the newly selected cost data will be updated to the display tab

5: Get project introduction:

    def selectIntroduction(self):
        introduction,ok = QInputDialog.getMultiLineText(self,"Project introduction","Introduction:","Service outsourcing third party company \nPython project")
        if ok :
            self.introductionLable.setText(introduction)
  • 1
  • 2
  • 3
  • 4

Call the getMultiLineText() function of QInputDialog to pop up the standard multiline text type input dialog box. The prototype of getMultiLineText() function is as follows:

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

The first parameter, parent, is used to specify the parent component;

The second parameter str, enter the title name of the dialog box;

The third parameter str is the label prompt of the input dialog box;

The fourth parameter text, enter the default value of LineEdit in the dialog box;

The fifth parameter flags indicates the form identification of the standard input dialog box;

The sixth parameter inputMethodHints realizes different keyboard layouts by selecting different inputMethodHints values;

Effect after clicking the introductionButton:

If the user clicks the "OK" button, the newly modified project introduction information will be updated to the display label

Posted by DragonFire-N[R] on Mon, 23 May 2022 07:17:34 +0300