简体   繁体   English

如何在PyQt5 InputDialog中获取更多输入文本?

[英]How can I get more input text in PyQt5 InputDialog?

在此输入图像描述

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. 我希望在PyQt5.QtWidgets QInputDialog中从用户那里获得多个输入文本...在这段代码中,我可以得到一个输入文本框,当我点击按钮时,我想获得更多输入文本框。 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_())

QInputDialog is a convenience class to retrieve a single input from the user. QInputDialog是一个便捷类,用于从用户检索单个输入。

If you want more fields, use a QDialog . 如果您想要更多字段,请使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM