Pablo Flores
Pablo Flores

Reputation: 717

Create a class object from another class in python

I am new in python, and I am trying to create an object in one class, and then delete that same object from another class.

This is part of the code:

class Win(QMainWindow):
 list_1 = []              #This lists are filled with objects
 list_2 = []
 def __init__(self):
   #A lot of stuff in here

   self.splitter = QSplitter(Qt.Vertical)

 def addObject(self):
   plot = Matplotlib()     #This is another class where i create a matplotlib figure   
   if len(Win.list_1) < 2:
     self.splitter.addWidget(plot)

So, with this, i create an object if the number of items in list_1 is lower than 3, and then i add it to list_1 and list_2, and i add it to the splitter of course. This works fine. Now, i create a method to delete this splitter (with the object inside of it too) like this:

  deleteObject(self):
    if len(Win.list_1) == 1:
      widget_erased = self.splitter.widget(index)
      widget_erased.hide()
      widget_erased.deleteLater()

As you can see, if i have 1 object, i can delete it. The problem comes when i have more objects. In this same method i write:

    if len(Win.list_1) > 1:
    #I open A QDialog where i see the names of the objects from the lists in a QListWidget
      self.delete = Delete()      
      self.delete.exec_()

Now, this is the class with the QDialog:

class Delete(self):
  def _init__(self):
    #A lot of stuff in here

  def deleteObjectCreated(self):
    #There are another things before the next lines

    widget_erased = Win.splitter.widget(index)   
    widget_erased.hide()
    widget_erased.deleteLater()

with this last method, i select the object in the QDialog, and when i press a button, the object is deleted from both lists, but the splitter remains, and i get this error:

type object "Win" has no attribute "splitter"

How can i accomplish this? I mean, delete the object that i choose from the QDialog, that is created in another class?

Hope you can help me.

Upvotes: 1

Views: 4239

Answers (2)

ekhumoro
ekhumoro

Reputation: 120738

The problem is that you are trying to access splitter via the class object rather than the instance object.

In your example, this can be solved by setting the instance object (i.e. self) as the parent of the Delete dialog:

class Win(QMainWindow):
    ...

    def deleteObject(self):
        ...
        if len(self.list_1) > 1:
            self.delete = Delete(self) # set self as the parent


class Delete(self):
    def _init__(self, parent):
        super(Delete, self).__init__(parent)
        ...

    def deleteObjectCreated(self):
        widget_erased = self.parent().splitter.widget(index)
        ...

Upvotes: 1

AlokThakur
AlokThakur

Reputation: 3741

'Win' is class object and this object is not having 'splitter' attribute. If I refer init of class 'Win' it looks like splitter is attribute of instance of 'Win' not the attribute of class 'Win'

self.splitter.QSplitter(Qt.Vertical) 

So you rightly getting error when you try to access Win.splitter.

This should be part of your design that splitter should be instance attribute or class attribute, I am not suggesting any thing on your design. But just to work your code If you want to access Win.splitter then do below change :-

def __init__(self):
   #A lot of stuff in here

   Win.splitter.QSplitter(Qt.Vertical)

or You can change def deleteObjectCreated(self) as below:-

def deleteObjectCreated(self, instance_win):
    instance_win.splitter.QSplitter(Qt.Vertical)

Upvotes: 0

Related Questions