How to get Buttons to work

Albert-Jan Roskam sjeik_appie at hotmail.com
Sat Apr 22 08:56:45 EDT 2017


(sorry for top-posting). UpdateRecords and the other functions need to be nested so they fall under your class. Right now they are functions, not methods.

AJ
________________________________
From: Python-list <python-list-bounces+sjeik_appie=hotmail.com at python.org> on behalf of horgan.anton5 at gmail.com <horgan.anton5 at gmail.com>
Sent: Saturday, April 22, 2017 12:45:09 PM
To: python-list at python.org
Subject: How to get Buttons to work

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()

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_())
--
https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list