苦苦于让我的按钮工作

时间:2017-04-22 08:59:47

标签: python python-3.x pyqt4

我正在忙着学习Python,我想创建一个连接到数据库的简单程序来查找Doctors的信息。现在,据我所知,一切正常,数据库连接,信息显示,但按钮不想工作。请参阅下面的代码和错误。

非常感谢任何帮助。

错误:

Traceback (most recent call last):
  File "C:\Users\Bl@h\Desktop\New INF\CallDoctorLocator.py", line 57, in <module>
    myapp = MyForm()
  File "C:\Users\Bl@h\Desktop\New INF\CallDoctorLocator.py", line 29, in __init__
    QtCore.QObject.connect(self.ui.ButtonUpdate, QtCore.SIGNAL('clicked()' ), self.UpdateRecords)
AttributeError: 'MyForm' object has no attribute 'UpdateRecords'
>>> 

代码:

#CallDoctorLocator import sys from DoctorLocator import * from PyQt4 import QtSql, QtGui

#Create Connection to the Database def createConnection():
    db = QtSql.QSqlDatabase.addDatabase('QMYSQL')
    db.setHostName('localhost')
    db.setDatabaseName('healthcare')
    db.setUserName('root')
    db.setPassword('3364834')
    db.open()
    print (db.lastError().text())
    return True

class MyForm(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.model = QtSql.QSqlTableModel(self)
        self.model.setTable("doctors")
        self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
        self.model.select()
        self.ui.tableView.setModel(self.model)
        QtCore.QObject.connect(self.ui.ButtonUpdate, QtCore.SIGNAL('clicked()' 
        ), self.UpdateRecords)
        QtCore.QObject.connect(self.ui.buttonCancel, QtCore.SIGNAL('clicked()' 
        ), self.CancelChanges)
        QtCore.QObject.connect(self.ui.buttonAdd, QtCore.SIGNAL('clicked()' ), 
        self.AddRecord)
        QtCore.QObject.connect(self.ui.buttonDelete, QtCore.SIGNAL('clicked()' 
        ), self.DeleteRecord)
        QtCore.QObject.connect(self.ui.buttonSearch, QtCore.SIGNAL('clicked()' 
        ), self.SearchRecords)


def UpdateRecords(self):
    self.model.AddRecord()

def CancelChanges(self):
    self.model.revertAll()

def UpdateRecords (self):
    self.model.insertRow(self.ui.tableView.currentIndex().row())

def DeleteRecords(self):
    self.model.removeRow(self.ui.tableView.currentIndex().row())
    self.model.AddRecord()

def SearchRecords(self):
    self.model.setFilter("Name like '" + self.ui.Name.text()+"%'")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

0 个答案:

没有答案