Python初学者错误
Posted
技术标签:
【中文标题】Python初学者错误【英文标题】:Python Beginners error 【发布时间】:2015-09-01 23:13:41 【问题描述】:我有这个非常“大”的代码
class ClassePrincipal(object):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName
MainWindow.setObjectName("MainWindow")
MainWindow.resize(464, 501)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 427, 719))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.gridLayout = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.gridLayout.setObjectName("gridLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.label_2 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_2.setObjectName("label_2")
self.verticalLayout.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_2.setObjectName("lineEdit_2")
self.verticalLayout.addWidget(self.lineEdit_2)
self.label_3 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_3.setObjectName("label_3")
self.verticalLayout.addWidget(self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_3.setObjectName("lineEdit_3")
self.verticalLayout.addWidget(self.lineEdit_3)
self.label_4 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_4.setObjectName("label_4")
self.verticalLayout.addWidget(self.label_4)
self.lineEdit_4 = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_4.setObjectName("lineEdit_4")
self.verticalLayout.addWidget(self.lineEdit_4)
self.label_5 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_5.setObjectName("label_5")
self.verticalLayout.addWidget(self.label_5)
self.lineEdit_5 = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_5.setObjectName("lineEdit_5")
self.verticalLayout.addWidget(self.lineEdit_5)
self.label_6 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_6.setObjectName("label_6")
self.verticalLayout.addWidget(self.label_6)
self.lineEdit_6 = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_6.setObjectName("lineEdit_6")
self.verticalLayout.addWidget(self.lineEdit_6)
self.label_7 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_7.setObjectName("label_7")
self.verticalLayout.addWidget(self.label_7)
self.dateEdit = QtWidgets.QDateEdit(self.scrollAreaWidgetContents)
self.dateEdit.setObjectName("dateEdit")
self.verticalLayout.addWidget(self.dateEdit)
self.label_8 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_8.setObjectName("label_8")
self.verticalLayout.addWidget(self.label_8)
self.lineEdit_8 = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_8.setObjectName("lineEdit_8")
self.verticalLayout.addWidget(self.lineEdit_8)
self.label_9 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_9.setObjectName("label_9")
self.verticalLayout.addWidget(self.label_9)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.radioButton_4 = QtWidgets.QRadioButton(self.scrollAreaWidgetContents)
self.radioButton_4.setObjectName("radioButton_4")
self.horizontalLayout_3.addWidget(self.radioButton_4)
self.radioButton_3 = QtWidgets.QRadioButton(self.scrollAreaWidgetContents)
self.radioButton_3.setObjectName("radioButton_3")
self.horizontalLayout_3.addWidget(self.radioButton_3)
self.radioButton_2 = QtWidgets.QRadioButton(self.scrollAreaWidgetContents)
self.radioButton_2.setObjectName("radioButton_2")
self.horizontalLayout_3.addWidget(self.radioButton_2)
self.radioButton = QtWidgets.QRadioButton(self.scrollAreaWidgetContents)
self.radioButton.setObjectName("radioButton")
self.horizontalLayout_3.addWidget(self.radioButton)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.label_10 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_10.setObjectName("label_10")
self.verticalLayout.addWidget(self.label_10)
self.lineEdit_7 = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_7.setObjectName("lineEdit_7")
self.verticalLayout.addWidget(self.lineEdit_7)
self.label_11 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_11.setObjectName("label_11")
self.verticalLayout.addWidget(self.label_11)
self.textEdit = QtWidgets.QTextEdit(self.scrollAreaWidgetContents)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.label_12 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_12.setObjectName("label_12")
self.verticalLayout.addWidget(self.label_12)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.graphicsView = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView.setObjectName("graphicsView")
self.horizontalLayout_4.addWidget(self.graphicsView)
self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_4.addWidget(self.pushButton)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.pushButton_2 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_5.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_5.addWidget(self.pushButton_3)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem)
self.pushButton_4 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_5.addWidget(self.pushButton_4)
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.pushButton_5 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_6.addWidget(self.pushButton_5)
self.comboBox = QtWidgets.QComboBox(self.scrollAreaWidgetContents)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.horizontalLayout_6.addWidget(self.comboBox)
self.lineEdit_9 = QtWidgets.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_9.setObjectName("lineEdit_9")
self.horizontalLayout_6.addWidget(self.lineEdit_9)
self.verticalLayout.addLayout(self.horizontalLayout_6)
self.gridLayout.addLayout(self.verticalLayout, 1, 0, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout_2.addWidget(self.scrollArea, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 464, 21))
self.menubar.setObjectName("menubar")
self.menuArquivos = QtWidgets.QMenu(self.menubar)
self.menuArquivos.setObjectName("menuArquivos")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionSalvar = QtWidgets.QAction(MainWindow)
self.actionSalvar.setObjectName("actionSalvar")
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setObjectName("actionClose")
self.actionClose_2 = QtWidgets.QAction(MainWindow)
self.actionClose_2.setObjectName("actionClose_2")
self.menuArquivos.addAction(self.actionSalvar)
self.menuArquivos.addAction(self.actionClose)
self.menuArquivos.addAction(self.actionClose_2)
self.menubar.addAction(self.menuArquivos.menuAction())
self.retranslateUi(MainWindow)
self.actionClose_2.triggered.connect(MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Nome do Solicitante"))
self.label_2.setText(_translate("MainWindow", "CNPJ"))
self.label_3.setText(_translate("MainWindow", "Email do Solicitante"))
self.label_4.setText(_translate("MainWindow", "Razão Social"))
self.label_5.setText(_translate("MainWindow", "Endereço do Solicitante"))
self.label_6.setText(_translate("MainWindow", "Endereço do Serviço"))
self.label_7.setText(_translate("MainWindow", "Data"))
self.label_8.setText(_translate("MainWindow", "Valor so Serviço"))
self.label_9.setText(_translate("MainWindow", "Forma de Pagamento"))
self.radioButton_4.setText(_translate("MainWindow", "RadioButton"))
self.radioButton_3.setText(_translate("MainWindow", "RadioButton"))
self.radioButton_2.setText(_translate("MainWindow", "RadioButton"))
self.radioButton.setText(_translate("MainWindow", "RadioButton"))
self.label_10.setText(_translate("MainWindow", "Equipamentos Usados"))
self.label_11.setText(_translate("MainWindow", "Descrição do Serviço"))
self.label_12.setText(_translate("MainWindow", "Imagem"))
self.pushButton.setText(_translate("MainWindow", "Procurar..."))
self.pushButton_2.setText(_translate("MainWindow", "Enviar via Email"))
self.pushButton_3.setText(_translate("MainWindow", "Salvar"))
self.pushButton_4.setText(_translate("MainWindow", "Cancelar"))
self.pushButton_5.setText(_translate("MainWindow", "Busca Banco de Dados"))
self.comboBox.setItemText(0, _translate("MainWindow", "CNPJ"))
self.comboBox.setItemText(1, _translate("MainWindow", "Nome do Solicitante"))
self.comboBox.setItemText(2, _translate("MainWindow", "Data"))
self.menuArquivos.setTitle(_translate("MainWindow", "Arquivos"))
self.actionSalvar.setText(_translate("MainWindow", "Salvar"))
self.actionClose.setText(_translate("MainWindow", "Apagar"))
self.actionClose_2.setText(_translate("MainWindow", "Close"))
def main():
app = QApplication(sys.argv)
Mw = ClassePrincipal()
Mw.show()
app.exec_()
if __name__ == '__main__':
main()
但是我有这个错误消息
Traceback (most recent call last):
File "C:/Users/Alexandre/Documents/Python/FinalmenteUltimo.py", line 221, in <module>
main()
File "C:/Users/Alexandre/Documents/Python/FinalmenteUltimo.py", line 215, in main
Mw = ClassePrincipal()
File "C:/Users/Alexandre/Documents/Python/FinalmenteUltimo.py", line 14, in __init__
self.setupUi(self)
File "C:/Users/Alexandre/Documents/Python/FinalmenteUltimo.py", line 17, in setupUi
MainWindow.setObjectName
AttributeError: 'ClassePrincipal' object has no attribute 'setObjectName'
但是,我一直在阅读这个模块,并且有这个属性。
谁能帮帮我,我对 python 真的很陌生,这是我的第一个程序,我不知道发生了什么。
从现在开始,谢谢你。
Ps:我用的是QT Designer。
【问题讨论】:
不应该ClassePrincipal
继承自某个QT 类吗?
是的,在原始代码中有一些 Import 语句
setupUi
在__init__
中没有第二个参数
我应该放什么?
【参考方案1】:
正如错误消息所暗示的,第 17 行有一个错误: 我猜你错误地写了两次'MainWindow.setObjectName',但第一次没有参数。因此,Python 正在“认为”您引用具有此名称的属性而不是方法,并且基础类没有这样的属性(如错误中所述)。
【讨论】:
对不起,我的真实代码没有这一行,我发错了,只有完整的一行 你知道,如果我评论这条正确的行,会发生第二个错误,同样的错误,但现在调整大小【参考方案2】:我相信这个文件是由 pyuic5 生成的。您应该避免手动编辑计算机生成的文件。如果你有 .ui 文件,你可以尝试使用下面的 pyuic5 链接生成 py 文件可能会帮助你
http://pyqt.sourceforge.net/Docs/PyQt5/designer.html
你能否让你的类扩展 QMainWindow 并将 MainWindow 的部分更改为 self ,如下面的 setupUi 中。如果这不起作用使用主窗口选项创建文件,请在 pyuic5 上使用 -x 标志,它将为您提供可执行的 python 代码,这是更好的选择。下面的链接也应该有帮助
self.setObjectName("MainWindow")
self.resize(464, 501)
http://www.rkblog.rk.edu.pl/w/p/simple-text-editor-pyqt4/
【讨论】:
我不编辑,我只是添加主要部分,和你发布的这个网页一样。【参考方案3】:我做到了,为了解决我刚刚做的问题
改为:
class ClassePrincipal(object):
我用过
class ClassePrincipal(QMainWindow):
还是谢谢你。
【讨论】:
以上是关于Python初学者错误的主要内容,如果未能解决你的问题,请参考以下文章