PyQt 输入对话框,意外类型
Posted
技术标签:
【中文标题】PyQt 输入对话框,意外类型【英文标题】:PyQt input dialog, unexpected type 【发布时间】:2018-03-23 11:47:52 【问题描述】:这是我第一次使用 QInputDialog 并且我坚持使用这个示例。 问题是当我按下 EditButton 并尝试打开 QInputDialog 时。 出现“意外类型”错误:
"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): 参数 1 具有意外类型 'Ui_Dia 记录'"
我正在按照书中的示例进行操作,但它似乎不起作用。 有人可以解释一下吗?
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_())
【问题讨论】:
报错说明这行代码有问题newtext, ok = QInputDialog.getText(self, "Enter new text", "Enter new text")
具体self
的使用是Ui_Dialog
类型为QWidget
?
我猜你的类应该是QWidget
的子类而不是object
你能举个例子给我解释一下吗?
【参考方案1】:
问题在于 QFileDialog 将 QWidget 项作为其父项作为其第一个参数。当你提供self
时,你提供给它的是一个继承自对象而不是QWidget 的Ui_Dialog
类的实例。
对此有几种不同的修复方法,其中涉及的代码更改很少:
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
【讨论】:
【参考方案2】:试试看:
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_())
【讨论】:
以上是关于PyQt 输入对话框,意外类型的主要内容,如果未能解决你的问题,请参考以下文章