【发布时间】:2016-02-04 13:32:36
【问题描述】:
我是 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 中选择的对象,它是在另一个类中创建的?
希望你能帮助我。
【问题讨论】: