0
from PyQt5 import QtCore, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(200, 40, 81, 20))
        self.label.setObjectName("label")
        self.lineedit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineedit.setGeometry(QtCore.QRect(270, 40, 113, 20))
        self.lineedit.setObjectName("lineedit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "label1:"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我想在我的 lineedit 下有一个 QInputDialog,所以我可以从输入对话框中选择“是”或“否”,我如何将它放入我的代码中?

另外我如何获得 inputdialog 的值?

谢谢!

4

1 回答 1

1

试试看:

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(200, 40, 81, 20))
        self.label.setObjectName("label")
        self.lineedit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineedit.setGeometry(QtCore.QRect(270, 40, 113, 20))
        self.lineedit.setObjectName("lineedit")
# +++
        self.btn = QtWidgets.QPushButton(self.centralwidget)
        self.btn.setGeometry(QtCore.QRect(200, 70, 81, 20))
        self.btn.setObjectName("btn")
        self.btn.clicked.connect(self.getTextInputDialog)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "label1:"))
        self.btn.setText(_translate("MainWindow", "InputDialog"))

# +++
    def getTextInputDialog(self):
        text, okPressed = QtWidgets.QInputDialog.getText(None, 
                                                        "Get text",
                                                        "Your name:", 
                                                        QtWidgets.QLineEdit.Normal, 
                                                        "")   
        if okPressed and text != '':
            print(text)
            ui.lineedit.setText(text)        



if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

在此处输入图像描述

于 2018-10-22T21:44:31.787 回答