Issue
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_())
Solution
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)
Answered By - Dimitry Ernot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.