我是新的python,我试图在一个类中创建一个对象,然后从另一个类中删除同一个对象。从python的另一个类创建一个类对象
这是代码的一部分:
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)
所以,用这一点,我创建一个对象,如果在LIST_1项的数目是低于3,并且然后我将它添加到LIST_1和list_2,和当然,我将它添加到分离器中。这工作正常。现在,我创造这样删除这个分路器(与它里面太的对象)的方法:
deleteObject(self):
if len(Win.list_1) == 1:
widget_erased = self.splitter.widget(index)
widget_erased.hide()
widget_erased.deleteLater()
正如你所看到的,如果我有1个对象,我可以将其删除。问题出现时,我有更多的对象。在这个同样的方法我写:
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_()
现在,这是与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()
利用这最后的方法,我选择在QDialog的对象,当我按下一个按钮,对象从两份名单中删除,但仍然分路器,我得到这个错误:
type object "Win" has no attribute "splitter"
我怎样才能做到这一点?我的意思是,删除我从QDialog中选择的对象,这是在另一个类中创建的?
希望你能帮助我。
非常感谢你的回答,这对我有用。 –