PyQt input dialog, unexpected type

2019-08-19 02:39发布

问题:

It's the first time I use QInputDialog and I'm stuck with this example. The probem is when I press the EditButton and try to open a QInputDialog. An 'unexpected type' error comes out with it:

"TypeError: QInputDialog.getText(QWidget, str, str, QLineEdit.EchoMode echo=QLine Edit.Normal, str text=QString(), Qt.WindowFlags flags=0, Qt.InputMethodHints inp utMethodHints=Qt.ImhNone) -> (str, bool): argument 1 has unexpected type 'Ui_Dia log'"

I'm following an example from a book but it seems not be working. Could someone explain it, please?

from PyQt5 import QtCore, QtGui, QtWidgets 
from PyQt5.QtWidgets import QInputDialog
class Ui_Dialog(object):
    def Addlist(self):
        self.listWidget.addItem(self.lineEdit.text())
        self.lineEdit.setText('')
        self.lineEdit.setFocus()
    def Editlist(self):
        row = self.listWidget.currentRow()
        newtext, ok = QInputDialog.getText(self, "Enter new text", "Enter new text")   
        if ok and (len(newtext) !=0):
            self.listWidget.takeItem(self.listWidget.currentRow())
            self.listWidget.insertItem(row, QListWidgetItem(newtext))      
    def Delete(self):
        self.listWidget.takeItem(self.listWidget.currentRow())
    def Deleteall(self):
        self.listWidget.clear()
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(570, 318)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(30, 20, 101, 21))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(130, 20, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.AddButton = QtWidgets.QPushButton(Dialog)
        self.AddButton.setGeometry(QtCore.QRect(150, 80, 75, 23))
        self.AddButton.setObjectName("AddButton")
        self.listWidget = QtWidgets.QListWidget(Dialog)
        self.listWidget.setGeometry(QtCore.QRect(290, 20, 256, 271))
        self.listWidget.setObjectName("listWidget")
        self.EditButton = QtWidgets.QPushButton(Dialog)
        self.EditButton.setGeometry(QtCore.QRect(150, 120, 75, 23))
        self.EditButton.setObjectName("EditButton")
        self.DeteleButton = QtWidgets.QPushButton(Dialog)
        self.DeteleButton.setGeometry(QtCore.QRect(150, 160, 75, 23))
        self.DeteleButton.setObjectName("DeteleButton")
        self.DeleteAllButton = QtWidgets.QPushButton(Dialog)
        self.DeleteAllButton.setGeometry(QtCore.QRect(150, 200, 75, 23))
        self.DeleteAllButton.setObjectName("DeleteAllButton")
        self.listWidget.addItem('Pizza')
        self.listWidget.addItem('Pasta')
        self.listWidget.addItem('Burrito')
        self.listWidget.addItem('Hamburger')
        self.AddButton.clicked.connect(self.Addlist)
        self.EditButton.clicked.connect(self.Editlist)
        self.DeteleButton.clicked.connect(self.Delete)
        self.DeleteAllButton.clicked.connect(self.Deleteall)
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Enter Text"))
        self.AddButton.setText(_translate("Dialog", "Add"))
        self.EditButton.setText(_translate("Dialog", "Edit"))
        self.DeteleButton.setText(_translate("Dialog", "Delete"))
        self.DeleteAllButton.setText(_translate("Dialog", "Delete All"))
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

回答1:

Try it:

from PyQt5 import QtCore, QtGui, QtWidgets 
from PyQt5.QtWidgets import QInputDialog,       QWidget, QListWidgetItem   # ++

class Ui_Dialog(QWidget):                                 # - object  + QWidget

    def Addlist(self):
        self.listWidget.addItem(self.lineEdit.text())
        self.lineEdit.setText('')
        self.lineEdit.setFocus()

    def Editlist(self):
        row = self.listWidget.currentRow()
        newtext, ok = QInputDialog.getText(self, "Enter new text", "Enter new text")  

        if ok and (len(newtext) !=0):
            self.listWidget.takeItem(self.listWidget.currentRow())
            self.listWidget.insertItem(row, QListWidgetItem(newtext)) 

    def Delete(self):
        self.listWidget.takeItem(self.listWidget.currentRow())

    def Deleteall(self):
        self.listWidget.clear()

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(570, 318)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(30, 20, 101, 21))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(130, 20, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.AddButton = QtWidgets.QPushButton(Dialog)
        self.AddButton.setGeometry(QtCore.QRect(150, 80, 75, 23))
        self.AddButton.setObjectName("AddButton")
        self.listWidget = QtWidgets.QListWidget(Dialog)
        self.listWidget.setGeometry(QtCore.QRect(290, 20, 256, 271))
        self.listWidget.setObjectName("listWidget")
        self.EditButton = QtWidgets.QPushButton(Dialog)
        self.EditButton.setGeometry(QtCore.QRect(150, 120, 75, 23))
        self.EditButton.setObjectName("EditButton")
        self.DeteleButton = QtWidgets.QPushButton(Dialog)
        self.DeteleButton.setGeometry(QtCore.QRect(150, 160, 75, 23))
        self.DeteleButton.setObjectName("DeteleButton")
        self.DeleteAllButton = QtWidgets.QPushButton(Dialog)
        self.DeleteAllButton.setGeometry(QtCore.QRect(150, 200, 75, 23))
        self.DeleteAllButton.setObjectName("DeleteAllButton")
        self.listWidget.addItem('Pizza')
        self.listWidget.addItem('Pasta')
        self.listWidget.addItem('Burrito')
        self.listWidget.addItem('Hamburger')
        self.AddButton.clicked.connect(self.Addlist)
        self.EditButton.clicked.connect(self.Editlist)
        self.DeteleButton.clicked.connect(self.Delete)
        self.DeleteAllButton.clicked.connect(self.Deleteall)
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Enter Text"))
        self.AddButton.setText(_translate("Dialog", "Add"))
        self.EditButton.setText(_translate("Dialog", "Edit"))
        self.DeteleButton.setText(_translate("Dialog", "Delete"))
        self.DeleteAllButton.setText(_translate("Dialog", "Delete All"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())



回答2:

The problem is that QFileDialog takes as its first argument a QWidget item as its parent. When you supply it self, you are supplying it with an instance of the Ui_Dialog class which inherits from object, not QWidget.

There are a few different fixes for this, one that involves very little code change is this:

def setupUi(self, Dialog):
    self.dialog = Dialog
    # your code here

def Editlist(self):
    row = self.listWidget.currentRow()
    newtext, ok = QInputDialog.getText(self.dialog, "Enter new text", "Enter new text")
    # your code heret