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,所以我可以从输入对话框中选择“是”或“否”,如何将其放入代码中?
还如何获取输入对话框的值?
谢谢!
答案 0 :(得分: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_())