AttributeError:'Ui_Dialog'对象没有属性'lineEdit_3'

时间:2017-08-22 06:30:05

标签: python pyqt pyqt4

链接到我的完整代码:https://www.dropbox.com/s/0tdnm2yd8038fwh/additem.py?dl=0

这是我得到的错误:

  File "C:/Users/Lloyd/Desktop/Python Projects/stock/additem.py", line 187, in <module>
    ui = Ui_Dialog()
  File "C:/Users/Lloyd/Desktop/Python Projects/stock/additem.py", line 23, in __init__
    self.setupUi(self)
  File "C:/Users/Lloyd/Desktop/Python Projects/stock/additem.py", line 66, in setupUi
    self.buttonBox.accepted.connect(self.accept())
  File "C:/Users/Lloyd/Desktop/Python Projects/stock/additem.py", line 169, in accept
    brandName = self.lineEdit_3.text()
AttributeError: 'Ui_Dialog' object has no attribute 'lineEdit_3'

在我添加之前一切顺利:

self.buttonBox.accepted.connect(self.accept())

调用此方法:

def accept(self):
    conn = sqlite3.connect('inventory.db')
    c = conn.cursor()

    unix = time.time()
    dateUpdated = datetime.datetime.fromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S')
    company = self.lineEdit_2.text()
    brandName = self.lineEdit_3.text()
    genericName = self.lineEdit_4.text()
    purchasePrice = self.lineEdit_5.text()
    category = self.lineEdit_6.text()
    sellingPrice = purchasePrice * sellingFactor
    quantity = self.lineEdit_7.text()
    #dosageForm = self.lineEdit_9.text()
    expiryDate = self.lineEdit_10.text()

    c.execute(
        "INSERT INTO inventory(dateUpdated, company, brandName, genericName, category, purchasePrice, sellingPrice, quantity, expiryDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
        (dateUpdated, company, brandName, genericName, category, purchasePrice, sellingPrice, quantity, expiryDate))
    conn.commit()

这是代码的其余部分:

app = QtGui.QApplication(sys.argv)
window = QtGui.QDialog()

ui = Ui_Dialog()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

当您将信号连接到插槽时,您必须代表插槽传递它,语法如下

sender.signal.connect(receiver.slot)

在您的情况下,您必须更改:

self.buttonBox.accepted.connect(self.accept())

为:

self.buttonBox.accepted.connect(self.accept)

注意:当您传递PyQt插槽名称时,您可以调用它,但如果您传递了已评估的函数,则无法执行此操作。