Reputation: 384
In Python 3.5, how can an object call a function in the object that instantiated it?
I've created a PyQt app that instantiates a simple database object, db. I want the db object to be able to update a progress bar in the main window object, but I can't figure out what to pass to the db object to make this possible.
I've spent hours reading online, but can't figure this out. I thought I could pass self
as the identifier of the MainWindow object to the db object, but that fails at db =photoDb(self)
with a NameError: "name 'self' is not defined". Clearly, I don't fully understand self
despite having read lots or web pages about it.
I suspect this must be a simple passing of information in the constructor, but I can't figure it out. (And I've spent hours reading StackOverflow entries that might relate to this. Sorry if this should be obvious or already answered in an entry I haven't found.) I'm using Python 3.5, PyQt4, and Ubuntu 16.10.
The gist of my code:
class photoDb(self, mainwindow):
def __init__(self):
self.db = []
def addPhotosToDb(self, filenames):
i = 0
for f in filenames:
(do a bunch of stuff here with f)
self.db.append(f)
mainwindow.updateProgressBar(int(100*i/len(filenames))
class MainWindow(QtGui.QMainWindow, photoOrg_MainWindow.Ui_MainWindow):
db =photoDb(self)
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
(lots more GUI widget connection code here)
def updateProgressBar(self, percentage):
self.progressBar.setValue(percentage)
def addPhotosToDb(self):
self.db.addPhotosToDb(listOfFiles) #the list is gotten from a dialog box generated elsewhere in the mainwindow class code
def main():
app = QtGui.QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
if __name__ == '__main__':
main()
Upvotes: 0
Views: 54
Reputation: 384
Thanks to @Gribouillis for pointing me in the right direction. Here's what worked.
Put the instantiation of the db object into the mainwindow's __init__()
method, ensuring that self.
precedes the db object's name so that other methods in the mainwindow class can access the db object.
In the db object, be sure to assign the mainwindow's ID (passed when it calls the db object) to a self. variable, so the methods outside the __init__()
class can access it too.
Here's the code that works:
class photoDb():
def __init__(self, mainwindow): #mainwindow is the name passed from the main object
self.db = []
self.mainwindow = mainwindow #store name of calling object so other methods in this class can use it
def addPhotosToDb(self, filenames):
i = 0
for f in filenames:
(do a bunch of stuff here with f)
self.db.append(f)
self.mainwindow.updateProgressBar(int(100*i/len(filenames))
class MainWindow(QtGui.QMainWindow, photoOrg_MainWindow.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.db =photoDb(self) #passing self lets the photoDb object store the calling object's name
(lots more GUI widget connection code here)
def updateProgressBar(self, percentage):
self.progressBar.setValue(percentage)
def addPhotosToDb(self):
self.db.addPhotosToDb(listOfFiles) #the list is gotten from a dialog box generated elsewhere in the mainwindow class code
def main():
app = QtGui.QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
if __name__ == '__main__':
main()
Upvotes: 0
Reputation: 2220
Move the db = photoDb(self)
in the MainWindow's __init__()
method.
Upvotes: 1