SoFunction
Updated on 2024-11-17

Pyqt5 basic interface components of the use of inputDialog

The QInputDialog class provides a simple aspect of the dialog box to get a single input from the user, which can be a string, an Int, a double or a drop-down list box entry.

The corresponding Dialog includes a prompt label, an input control (a QLineEdit if a string input box is invoked, a QSpinBox if an Int or double type is invoked, and a QComboBox if a list-entry input box is invoked), and a OK input (Ok) button and a Cancel input (Cancel) button.

QInputDialog:

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

QInputDialog also inherits from QDialog and provides a simple input dialog.

Code Example :

The sample code is as follows:

#-*- coding:utf-8 -*-
'''
inputDialog
'''
__author__ = 'Tony Zhu'

from  import QApplication, QWidget, QLineEdit, QInputDialog, QGridLayout, QLabel, QPushButton, QFrame

class InputDialog(QWidget):
  def __init__(self):    
    super(InputDialog,self).__init__()
    ()

  def initUi(self):
    ("Project information")
    (400,400,300,260)

    label1=QLabel("Project name:")
    label2=QLabel("Project type:")
    label3=QLabel("Project personnel:")
    label4=QLabel("Project Costs:")
    label5=QLabel("Project Description:")

     = QLabel("PyQt5")
    (|)
     = QLabel("Outsourcing")
    (|)
     = QLabel("40")
    (|)
     = QLabel("400.98")
    (|)
     = QLabel("Third-party company for outsourcing of services")
    (|)

    nameButton=QPushButton("...")
    ()
    styleButton=QPushButton("...")
    ()
    numberButton=QPushButton("...")
    ()
    costButton=QPushButton("...")
    ()
    introductionButton=QPushButton("...")
    ()

    mainLayout=QGridLayout()
    (label1,0,0)
    (,0,1)
    (nameButton,0,2)
    (label2,1,0)
    (,1,1)
    (styleButton,1,2)
    (label3,2,0)
    (,2,1)
    (numberButton,2,2)
    (label4,3,0)
    (,3,1)
    (costButton,3,2)
    (label5,4,0)
    (,4,1)
    (introductionButton,4,2)

    (mainLayout)



  def selectName(self):
    name,ok = (self,"Project name","Enter the name of the project:",
                    ,())
    if ok and (len(name)!=0):
      (name)
  def selectStyle(self):
    list = ["Outsourcing","Self-research"]

    style,ok = (self,"Nature of the project","Please select the nature of the project:",list)
    if ok :
      (style)

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

  def selectCost(self):
    cost,ok = (self,"Project costs","Please enter the number of project members:",float(()),100.00,500.00,2)
    if ok :
      (str(cost))

  def selectIntroduction(self):
    introduction,ok = (self,"Introduction to the project","Introduction:","Service Outsourcing Third Party Company \nPython project")
    if ok :
      (introduction)



if __name__=="__main__":
  import sys
  app=QApplication()
  myshow=InputDialog()
  ()
  (app.exec_())

The effect after running:

Example Description:

Select different types of input dialogs by clicking on different buttons to select the desired data.

Code Analysis:

L18~22:

    label1=QLabel("Project name:")
    label2=QLabel("Project type:")
    label3=QLabel("Project personnel:")
    label4=QLabel("Project Costs:")
    label5=QLabel("Project Description:")

A label that defines the name of the data item.

L24~33:

     = QLabel("PyQt5")
    (|)
     = QLabel("Outsourcing")
    (|)
     = QLabel("40")
    (|)
     = QLabel("400.98")
    (|)
     = QLabel("Third-party company for outsourcing of services")
    (|)

Defines the data content in the item data item, which is displayed in the corresponding label.

setFrameStyle() sets the style of the label with the following styles:

L35~L44:

    nameButton=QPushButton("...")
    ()
    styleButton=QPushButton("...")
    ()
    numberButton=QPushButton("...")
    ()
    costButton=QPushButton("...")
    ()
    introductionButton=QPushButton("...")
    ()

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

L46~61:

Instantiates the grid layout and adds the corresponding controls to the grid layout.

Functional Analysis:

1: Get the name of the project:

  def selectName(self):
    name,ok = (self,"Project name","Enter the name of the project:", ,())
    if ok and (len(name)!=0):
      (name)

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

 | getText(...)
 |   (QWidget, str, str,  echo=, str text=QString(),  flags=0,  inputMethodHints=) -> (str, bool)

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

The 2nd parameter, str, is the title name of the standard input dialog;

The 3rd parameter str, the label prompt for the standard input dialog;

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

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

The sixth parameter, flags, specifies the form identifier for the standard input dialog box;

The seventh parameter, inputMethodHints, enables different keyboard layouts by selecting different inputMethodHints values.

The effect after clicking the nameButton:

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

2: Get item properties:

  def selectStyle(self):
    list = ["Outsourcing","Self-research"]
    style,ok = (self,"Nature of the project","Please select the nature of the project:",list)
    if ok :
      (style)

Call QInputDialog's getItem() function to pop up the standard entry selection dialog box, getItem() function prototype is as follows:

 | getItem(...)
 |   (QWidget, str, str, list-of-str, int current=0, bool editable=True,  flags=0,  inputMethodHints=) -> (str, bool)

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

The 2nd parameter, str, is the title name of the standard entry selection dialog;

The 3rd parameter str, the label prompt for the standard entry selection dialog;

The fourth parameter list-of-str, the list of corresponding entries in the standard entry selection dialog;

The fifth parameter editable, the standard entry selects whether or not the dialog box entry is editable identification, the default is not editable;

The sixth parameter, flags, specifies the form identifier for the standard input dialog box;

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

The effect after clicking the styleButton:

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

3: Acquisition of project members:

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

Call QInputDialog's getInt() function to pop up the standard int type input dialog box, getInt() function prototype is as follows:

| getInt(...)
|   (QWidget, str, str, int value=0, int min=-2147483647, int max=2147483647, int step=1,  flags=0) -> (int, bool)

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

The 2nd parameter, str, is the title name of the standard int type input dialog;

The 3rd parameter str, the label prompt for the standard int type input dialog;

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

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

The sixth parameter, max, is the maximum value in the standard int type input dialog;

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

The 8th parameter, inputMethodHints, enables different keyboard layouts by selecting different inputMethodHints values;

The effect after clicking the numberButton:

If the user clicks the OK button, the newly selected member data is updated to the display tab.

4: Access to project costs:

  def selectCost(self):
    cost,ok = (self,"Project costs","Please enter the number of project members:",float(()),100.00,500.00,2)
    if ok :
      (str(cost))

Call QInputDialog's getDouble() function to pop up the standard float type input dialog box, getDouble() function prototype is as follows:

 | getDouble(...)
 |   (QWidget, str, str, float value=0, float min=-2147483647, float max=2147483647, int decimals=1,  flags=0) -> (float, bool)

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

The 2nd parameter, str, inputs the title name of the dialog box;

The third parameter, str, inputs the label prompt for the dialog box;

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

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

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

The seventh parameter decimals, the number of digits to be retained after the decimal point;

The 8th parameter, inputMethodHints, enables different keyboard layouts by selecting different inputMethodHints values;

The effect after clicking the costButton:

If the user clicks the OK button, the newly selected cost data is updated to the display tab.

5: Get a description of the project:

  def selectIntroduction(self):
    introduction,ok = (self,"Introduction to the project","Introduction:","Service Outsourcing Third Party Company \nPython project")
    if ok :
      (introduction)

Call QInputDialog's getMultiLineText() function to pop up the standard multi-line text type input dialog box, getMultiLineText() function prototype is as follows:

 | getMultiLineText(...)
 |   (QWidget, str, str, str text='',  flags=0,  inputMethodHints=) -> (str, bool)

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

The 2nd parameter, str, inputs the title name of the dialog box;

The third parameter, str, inputs the label prompt for the dialog box;

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

The fifth parameter, flags, specifies the form identifier for the standard input dialog;

The sixth parameter, inputMethodHints, enables different keyboard layouts by selecting different inputMethodHints values.

The effect after clicking the introductionButton:

If the user clicks the "OK" button, the newly modified project description information is updated to the display tab.

Above this Pyqt5 basic interface components of inputDialog is all I have shared with you, I hope to give you a reference, and I hope you support me more.