How to get Buttons to work

Peter Otten __peter__ at web.de
Sat Apr 22 09:06:07 EDT 2017


horgan.anton5 at gmail.com wrote:

> I am busy learning Python and I want to make a simple program that
> connects to a database to locate the information of Doctors. Now the as
> far as I can see everything works fine, database connects, info gets
> displayed, but the buttons don't want to work. Please see code and error
> below.
> 
> Any help will be greatly appreciated.
> 
> Error:
> 
> Traceback (most recent call last):
>   File "C:\Users\Bl at h\Desktop\New INF\CallDoctorLocator.py", line 57, in
>   <module>
>     myapp = MyForm()
>   File "C:\Users\Bl at 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'
>>>> 
> Code:
> 
> #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()

The above UpdateRecord() method (also the methods that follow) has to be 
indented to the same level as the __init__() method. As written it is 
defined as a standalone function.

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

Once the indentation is fixed: you cannot have two methods with the same 
name. You have to decide if you want the first or the second UpdateRecords() 
implementation.
> 
> 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_())





More information about the Python-list mailing list