Python MainWindow类无法找到属性

问题描述:

我正在构建一个GUI应用程序,我一直在试图调试代码并测试它,但它一直抛出我这个错误说。我为MainWindow类添加了一个按钮函数,它是'self.AddtoCart.clicked.connect(self.addCart)',但现在它不会打开。Python MainWindow类无法找到属性

'MainWindow'对象没有属性'addCart'。

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): 
def __init__(self, parent=None): 
    QtWidgets.QMainWindow.__init__(self, parent=parent) 
    self.setupUi(self) 
    self.Addtocart.clicked.connect(self.addCart) 



def addcart(self): 
    style = self.comboBox_6.currentText() 
    name = self.lineEdit_10.text() 
    color = self.lineEdit_11.text() 
    size = self.comboBox_7.currentText() 
    text = "style: {style}, name: {name}, color: {color}, size: {size}".format(style=style, name=name, color=color, size=size) 
    self.plainTextEdit.appendPlainText(text) 
+1

请缩进你的代码,缩进在Python中很重要。 – eyllanesc

在您的代码中,addcart()不在MainWindow的范围内,因为它处于相同的缩进级别。使用tab和addCart()缩进函数将成为该类的一个属性。

+1

此外,该方法被称为addcart,但该按钮连接到addCart。 Python区分大小写。 – SKoczian