4

enter image description here

I want get more than one input text from user in PyQt5.QtWidgets QInputDialog ... in this code I can just get one input text box and I want get more input text box when I was clicked the button. See the picture to more information ...

from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QLineEdit,QInputDialog,QHBoxLayout)
import sys

class FD(QWidget):
    def __init__(self):
        super().__init__()
        self.mysf()

    def mysf(self):
        hbox = QHBoxLayout()
        self.btn = QPushButton('ClickMe',self)
        self.btn.clicked.connect(self.sd)
        hbox.addWidget(self.btn)
        hbox.addStretch(1)

        self.le = QLineEdit(self)
        hbox.addWidget(self.le)

        self.setLayout(hbox)

        self.setWindowTitle("InputDialog")
        self.setGeometry(300,300,290,150)
        self.show()
    def sd(self):
        text , ok = QInputDialog.getText(self,'InputDialog','EnterYourName = ')
        if ok:
            self.le.setText(str(text))
if __name__ == '__main__':
    app = QApplication(sys.argv)
    F = FD()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jack Parten
  • 59
  • 2
  • 11

2 Answers2

14

QInputDialog is a convenience class to retrieve a single input from the user.

If you want more fields, use a QDialog.

For example:

class InputDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.first = QLineEdit(self)
        self.second = QLineEdit(self)
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self);

        layout = QFormLayout(self)
        layout.addRow("First text", self.first)
        layout.addRow("Second text", self.second)
        layout.addWidget(buttonBox)

        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

    def getInputs(self):
        return (self.first.text(), self.second.text())

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    dialog = InputDialog()
    if dialog.exec():
        print(dialog.getInputs())
    exit(0)
Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37
2

Here's is a general solution, which accepts any number of inputs, based on Dimitry Ernot's answer.

from PyQt5.QtWidgets import QApplication, QLineEdit, QDialogButtonBox, QFormLayout, QDialog
from typing import List

class InputDialog(QDialog):
    def __init__(self, labels:List[str], parent=None):
        super().__init__(parent)
        
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
        layout = QFormLayout(self)
        
        self.inputs = []
        for lab in labels:
            self.inputs.append(QLineEdit(self))
            layout.addRow(lab, self.inputs[-1])
        
        layout.addWidget(buttonBox)
        
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)
    
    def getInputs(self):
        return tuple(input.text() for input in self.inputs)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    dialog = InputDialog(labels=["First","Second","Third","Fourth"])
    if dialog.exec():
        print(dialog.getInputs())
    exit(0)
Diogo
  • 590
  • 6
  • 23